Upstream: ip_hash and backup

I’m using the upstream module to load balance between 2 back-end
servers, using ip_hash to effect a kind of ‘stickiness’ (making it more
likely that subsequent requests from the same client will go to the same
back-end server). Now I want to add a 3rd back-end server into the mix,
but which will only come into play if the other 2 are failing. There’s a
handy ‘backup’ parameter I can use for this, except that according to
the docs it cannot be combined with the ip_hash directive. I’m wondering
whether there is some other way to achieve what I want here?
Effectively, the ip_hash becomes irrelevant once it’s failed over to
this backup server as there is only one.

Hello!

On Thu, Jun 30, 2011 at 07:35:24PM +0100, John M. wrote:

only one.
Use error_page based fallback instead, i.e. something like this:

upstream backends {
    ip_hash;
    server 10.0.0.1;
    server 10.0.0.2;
}

upstream backup {
    server 10.0.0.3;
}

server {
    ...

    location / {
        error_page 502 504 = @fallback;
        proxy_pass http://backends;
    }

    location @fallback {
        proxy_pass http://backup;
    }
}

Maxim D.

Hello!

On Thu, Jun 30, 2011 at 08:31:35PM +0100, John M. wrote:

failing. There’s a handy ‘backup’ parameter I can use for this,
server 10.0.0.2;
error_page 502 504 = @fallback;
version of nginx? I’m running 0.7.65.
Named locations are available starting from 0.6.6, so 0.7.65
should be fine. You may want to upgrade anyway though, 0.7.65 is
rather old and not really supported.

Maxim D.

On 30/06/11 19:49, Maxim D. wrote:

failing. There’s a handy ‘backup’ parameter I can use for this,
server 10.0.0.1;
location / {

That’s just what I need, thanks. Is this dependent on some recent
version of nginx? I’m running 0.7.65.

John

Hello,
i have this little solution of load balancer at the front end using
nginx
and two php-fpm application servers at the backend and i installed
php-fpm
on the load balancer as a backup application server, i wanted to use
ip_hash
with backup i tried your solution but it keep giving me 502 bad gateway
errors.
this is my configuration
upstream apps {
ip_hash;
server 10.128.149.234:9000;
server 10.128.151.32:9000;
}
upstream backup {
server unix:/var/run/php5-fpm.sock;
}

server {

    listen 80 default_server;
    index index.php;
    root /var/www/wp;
    server_name .example.com;
    set $mreq 1;
    if ( $request_method = POST ){
            set $mreq 0;
    }
    if ( $uri ~ "/wp-" ) {

set $mreq 0;
}
if ($http_cookie ~*
“comment_author_|wordpressuser_|wp-postpass_|wordpre
ss_logged_in_” ) {
set $mreq 0;
}

    location / {

            default_type text/html;
            fastcgi_index index.php;
            include fastcgi_params;
            index index.php;
            if ( $mreq = 1){
                    add_header uri $request_uri;
                    set $memcached_key

data-$scheme://$host$request_uri;
memcached_pass 104.131.208.224:11211;
error_page 404 500 502 = @fallback;
}

            if ( $uri ~ \.php$ ) {
            fastcgi_pass apps;
            error_page 502 504 = @fallbackbackup;
                                      }
    }
    location @fallback {

            if ( $uri ~ \.php$ ) {
            fastcgi_pass apps;
            error_page 502 504 = @fallbackbackup;

    }
    }
    location @fallbackbackup {
            fastcgi_pass backup;

}
}

Posted at Nginx Forum: