Upstream fail over?

Hi,

Is it possible to have nginx proxy to upstream_a and if that fails then
default to upstream_b?

What I would like to accomplish is to have a Varnish upstream, and if
that’s down, then nginx should just proxy to the application backend
directly.

Thanks.

Morten

Posted at Nginx Forum:

Hello,

Yes, very easily with 0.7.x and above. It would be something like
below ( check Module ngx_http_core_module )

location / {
try_files @varnish @application;
}

location @varnish {
// proxy to varnish
}

location @application {
// proxy to application
}

– Merlin

merlin corey wrote:

Hello,

Yes, very easily with 0.7.x and above. It would be something like
below ( check Module ngx_http_core_module )

location / {
try_files @varnish @application;
}

location @varnish {
// proxy to varnish
}

location @application {
// proxy to application
}

– Merlin

I tried a config like this on 0.8.6 and I found that all requests ended
up going to the @application proxy, skipping the @varnish proxy
entirely. Am I doing something wrong?

Here’s a sample of my config:

upstream varnishservers
{
server 10.1.1.1:80;
}

upstream originservers
{
server 10.1.1.2:80;
server 10.1.1.2:80;
}

server
{
listen 8080;
server_name _;
server_name_in_redirect off;

location /

{
try_files @vanish @origin;
}
location @varnish
{
proxy_pass http://varnishservers;
proxy_set_header Host $host;
proxy_connect_timeout 3;
proxy_next_upstream error timeout;
}

location @origin
{
proxy_pass http://originservers;
proxy_set_header Host $host;
proxy_connect_timeout 3;
proxy_next_upstream error timeout;
}

}

Thanks!

Mahiti Support wrote:

Mitchua M. wrote:

I tried a config like this on 0.8.6 and I found that all requests ended
up going to the @application proxy, skipping the @varnish proxy
entirely. Am I doing something wrong?

Here’s a sample of my config:

location /

{
try_files @vanish @origin;
}

Thanks!

Hi,

You seem to have put it as @vanish instead of @varnish.
Is that causing you the issue?

Vishnu

Sorry, bad copy/paste job :wink: Those are correct in the config I’m using.

Any ideas? Does that look right?

Mitchua M. wrote:

I tried a config like this on 0.8.6 and I found that all requests ended
up going to the @application proxy, skipping the @varnish proxy
entirely. Am I doing something wrong?

Here’s a sample of my config:

location /

{
try_files @vanish @origin;
}

Thanks!

Hi,

You seem to have put it as @vanish instead of @varnish.
Is that causing you the issue?

Vishnu

Mitchua M. wrote:

Mahiti Support wrote:

Mitchua M. wrote:

I tried a config like this on 0.8.6 and I found that all requests ended
up going to the @application proxy, skipping the @varnish proxy
entirely. Am I doing something wrong?

Here’s a sample of my config:

location /

{
try_files @vanish @origin;
}

Thanks!

Hi,

You seem to have put it as @vanish instead of @varnish.
Is that causing you the issue?

Vishnu

Sorry, bad copy/paste job :wink: Those are correct in the config I’m using.

Any ideas? Does that look right?

I just tried this and the root document of the website hits the varnish
servers, but every other request doesn’t. Could there be a pathing
problem going on here?

location /
{
try_files $uri $uri/ @origin;
proxy_pass http://varnishservers;
proxy_set_header Host $host;
proxy_connect_timeout 3;
proxy_next_upstream error timeout;
}