What I would like to have is nginx first to try primary service and then to fallback to the backup if primary is not available.<br><br>What I've tried:<br><br>1. try_files:<br><br><br> location /api { <br> try_files @primary @backup; <br>
}<br> <br> <br> location @primary {<br> proxy_pass <a href="http://localhost:81" target="_blank">http://localhost:81</a>; <br> }<br><br> location @backup {<br> include /etc/nginx/fastcgi_params;<br>
fastcgi_param SCRIPT_FILENAME $document_root/boot/api.php;<br> fastcgi_pass unix:/tmp/php-fpm.sock;<br> }<br><br>This results in nginx only invoking @backup.<br>If I switch try_files params (try_files @backup @primary) then @primary is invoked but in case it's not running I get 502 error (error log: 9228#0: *1850 no live upstreams while connecting to upstream).<br>
<br><br>2. upstream<br><br>upstream backend {<br> server localhost:81;<br> server unix:/tmp/php-fpm.sock;<br>}<br><br>location /api { <br> proxy_pass <a href="http://backend" target="_blank">http://backend</a>; <br>
}<br><br>The first part of this solution works -- requests are first passed to localhost:81 and handled there without any problems and if this one is down requests go to the second server in the upstream.<br>What happens there is another 502 error:<br>
error-log: *1840 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: ......, server: ......, request: "GET /api HTTP/1.1", upstream: "http://unix:/tmp/php-fpm.sock:/api"<br>
<br>How to configure fastcgi backend propery to handle this properly?<br><br><br><br>Is there any way to make this work?<br>If both are possible, which one is better?<br><br><br><br>-- <br>Denis Arh<br>