Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a node web app with nginx acting as a proxy.

Here's the server block for that site :

server {
        listen 80;

        server_name subdomain.example.org;

        location / {
                # Offline handling
                error_page https://example.org/502

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://127.0.0.1:4567;
                proxy_redirect off;

                # Socket.io Support
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

If the web app ever goes down, then the error_page directive sends the user off-site, but the url ends up changing. Is it possible to send the user to that site, but maintain the same url in the browser?

share|improve this question

1 Answer 1

As the doc says :

If there is no need to change URI during internal redirection it is possible to pass error processing into a named location:

location / {
    error_page 404 = @fallback; }

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

You would also need to put on the proxy_intercept_errors directive.

Also, to respect the URL format you give in your example (https://example.org/502), i had to use a $folder variable.

Finally, you will have to setup the resolver directive.

To conclude, you will have something like this :

location / {
   proxy_intercept_errors on;
   error_page 404 = @fallback;
   proxy_pass        http://127.0.0.1:4567;
   ...
   ...
}

location @fallback {
   resolver 8.8.8.8;   # <-- Put your own DNS Server here
   set $folder "502";
   proxy_pass https://example.org/$folder/;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.