I will not even try to explain why I've tried the "hard" way first :)<br><br>Your solution It worked perfectly.<br><br>I've added "error_log /dev/null crit;" to "location /api {" so it does not fill the log files with connection refused"<br>
<br><br>Thank you.<br><br><br><div class="gmail_quote">On Sun, Feb 14, 2010 at 1:55 PM, Maxim Dounin <span dir="ltr"><<a href="mailto:mdounin@mdounin.ru">mdounin@mdounin.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hello!<br>
<div class="im"><br>
On Sun, Feb 14, 2010 at 01:43:55PM +0100, Denis Arh wrote:<br>
<br>
> What I would like to have is nginx first to try primary service and then to<br>
> 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>
</div>This won't work as try_files tris *files* (surprise!) and uses<br>
fallback uri if no one was found. See syntax details here:<br>
<br>
<a href="http://wiki.nginx.org/NginxHttpCoreModule#try_files" target="_blank">http://wiki.nginx.org/NginxHttpCoreModule#try_files</a><br>
<br>
[...]<br>
<div class="im"><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<br>
> localhost:81 and handled there without any problems and if this one is down<br>
> requests go to the second server in the upstream.<br>
<br>
</div>Not really. Requests are randomly balanced between two servers<br>
you specified. Once one of them down it uses another one to<br>
process requests.<br>
<div class="im"><br>
> What happens there is another 502 error:<br>
> error-log: *1840 recv() failed (104: Connection reset by peer) while reading<br>
> response header from upstream, client: ......, server: ......, request: "GET<br>
> /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>
</div>You use proxy_pass, and all backends should be http. Using<br>
fastcgi backend in one pool with http ones isn't an option.<br>
<div class="im"><br>
> Is there any way to make this work?<br>
> If both are possible, which one is better?<br>
<br>
</div> location /api {<br>
error_page 502 504 = @fallback;<br>
proxy_pass <a href="http://primary-backend" target="_blank">http://primary-backend</a>;<br>
}<br>
<br>
location @fallback {<br>
fastcgi_pass ...<br>
}<br>
<br>
Maxim Dounin<br>
<br>
_______________________________________________<br>
nginx mailing list<br>
<a href="mailto:nginx@nginx.org">nginx@nginx.org</a><br>
<a href="http://nginx.org/mailman/listinfo/nginx" target="_blank">http://nginx.org/mailman/listinfo/nginx</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Denis Arh<br>