Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx proxy_next_upstream doesn't work

Tags:

nginx

I want nginx to search my local host for the file first and on a 404 error it should search server 1.1.1.1.

I am able to fetch the file that is located on local host, but not able to get from server 1.1.1.1.

server {
    listen 80;
    server_name  localhost;

    access_log /var/log/nginx/access.log main;

    location /products/ {
        proxy_next_upstream http_404;

        root /var/foo;
    }
}

server {
    listen 80;
    server_name  1.1.1.1;
    location /products/ {
        #########
    }
}

I guess proxy_next_upstream is not switching to the server.

Any help on this would be appreciated.

like image 285
sorabh Avatar asked Oct 13 '12 00:10

sorabh


1 Answers

The proxy_next_upstream directive is a configuration directive to control re-request from a group of upstream servers by a proxy_pass if request to one of them fails. It doesn't make sense without proxy_pass and an upstream block defined. You may use it if you proxy to multiple upstream servers like this:

upstream backends {
    server 192.2.0.1;
    server 192.2.0.2;
    ...
}

server {
    ...

    location / {
        proxy_pass http://backends;
        proxy_next_upstream error timeout http_404;
    }
}

If you want nginx to search for a file on disk, and if it's not found - proxy request to another server, configure it e.g. using try_files fallback instead:

location / {
    root /path/to/root;
    try_files $uri @fallback;
}

location @fallback {
    proxy_pass http://...
}

See http://nginx.org/r/try_files for more info about the try_files directive.

like image 191
Maxim Dounin Avatar answered Sep 23 '22 13:09

Maxim Dounin