Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback NGINX location

I’m new to NGINX and I am migrating a server. I haven’t finished everything on the new server, so I want it to match to the new server, unless that resource or path doesn’t exist. If so, I want to send it to the old server. Is there way to do that?

like image 460
dkimot Avatar asked Apr 17 '18 01:04

dkimot


People also ask

How does Nginx match location?

To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file.

Where is Nginx config file located?

By default, the configuration file is named nginx. conf and placed in the directory /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx .

What does location mean in nginx?

The nginx location directive is used to tell the nginx where to look for the resources including folders and files, at the time of matching URI against the block. Location directive block will be placed inside into the block of the server or inside into another block.

Where is Nginx Webroot?

By default, NGINX webroot is located at /var/www/html.


1 Answers

I did this by a hack with proxy_next_upstream

Define a upstream, forward most of reqeusts to new_server by controlling the weight, proxy_next_upstream will retry to forward the failed request to next server (old_server)

upstream backend {
    server new_server weight=10000;
    server old_server weight=1;
}

server {
    location / {
        proxy_pass http://backend;
        proxy_next_upstream error timeout http_404 http_500 http_502 http_503 http_504 non_idempotent;
    }
}

===========

Solution II

server {
    location / {
        proxy_pass http://new_server;
        error_page 404 500 502 503 504 = @fallback;
    }

    location @fallback {
        proxy_pass http://old_server;
    }
}
like image 68
Larry.He Avatar answered Sep 28 '22 02:09

Larry.He