I will not even try to explain why I&#39;ve tried the &quot;hard&quot; way first :)<br><br>Your solution It worked perfectly.<br><br>I&#39;ve added &quot;error_log /dev/null crit;&quot; to &quot;location /api {&quot; so it does not fill the log files with connection refused&quot;<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">&lt;<a href="mailto:mdounin@mdounin.ru">mdounin@mdounin.ru</a>&gt;</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>
&gt; What I would like to have is nginx first to try primary service and then to<br>
&gt; fallback to the backup if primary is not available.<br>
&gt;<br>
&gt; What I&#39;ve tried:<br>
&gt;<br>
&gt; 1. try_files:<br>
&gt;<br>
&gt;<br>
&gt;     location /api {<br>
&gt;         try_files @primary @backup;<br>
<br>
</div>This won&#39;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>
&gt; 2. upstream<br>
&gt;<br>
&gt; upstream backend {<br>
&gt;     server localhost:81;<br>
&gt;     server unix:/tmp/php-fpm.sock;<br>
&gt; }<br>
&gt;<br>
&gt; location /api {<br>
&gt;    proxy_pass <a href="http://backend" target="_blank">http://backend</a>;<br>
&gt; }<br>
&gt;<br>
&gt; The first part of this solution works -- requests are first passed to<br>
&gt; localhost:81 and handled there without any problems and if this one is down<br>
&gt; 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>
&gt; What happens there is another 502 error:<br>
&gt; error-log: *1840 recv() failed (104: Connection reset by peer) while reading<br>
&gt; response header from upstream, client: ......, server: ......, request: &quot;GET<br>
&gt; /api HTTP/1.1&quot;, upstream: &quot;http://unix:/tmp/php-fpm.sock:/api&quot;<br>
&gt;<br>
&gt; 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&#39;t an option.<br>
<div class="im"><br>
&gt; Is there any way to make this work?<br>
&gt; 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>