Nginx reverse proxy error page

Lormayna asked:

I’m using nginx as reverse proxy for a single machine.
I would like to have an error page when the backend machine goes down.

This is my configuration file:

server {
    listen   80;
    access_log  /var/log/nginx/access.log;
    root /var/www/nginx;
    error_page 403 404 500 502 503 504 /error.html;
    location / {
            include         /etc/nginx/proxy.conf;

My answer:

Aha, I see the problem. You have provided no way for nginx to actually serve static files such as /error.html, so it is trying to pass them upstream to your backend.

The quick fix would be:

location /error.html {

This will cause nginx to handle /error.html itself. It will then try to serve the file out of the defined document root.

By the way, you probably want to use different error pages for 4xx errors and 5xx errors. “Not found” or whatever is not what you want people (or search engines!) to see if a backend is down temporarily.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.