10

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 / {
            proxy_pass      http://192.168.1.78/;
            include         /etc/nginx/proxy.conf;
    }
quanta
  • 51,413
  • 19
  • 159
  • 217
Lormayna
  • 121
  • 1
  • 1
  • 4
  • You already do. What is the problem? – Michael Hampton Sep 11 '12 at 14:01
  • 1
    I receive a 502 "Bad gateway" instead that error.html – Lormayna Sep 11 '12 at 14:17
  • This is an error: 2012/09/11 16:32:10 [error] 5544#0: *7 connect() failed (110: Connection timed out) while connecting to upstream, client: 192.168.1.112, server: hansolo, request: "GET /error.html HTTP/1.1", upstream: "http://192.168.1.78:80/error.html", host: "192.168.1.68" – Lormayna Sep 11 '12 at 14:31

1 Answers1

10

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 {
    internal;
}

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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • You can template the `/error.html` file, and have it look differently depending on the error code. – Qsiris Feb 15 '19 at 13:55
  • Better to have different `error_page`s for each error, though I suspect something like that is what you had in mind. – Michael Hampton Feb 15 '19 at 15:50
  • This is what I had in mind (not my blog): https://blog.adriaan.io/one-nginx-error-page-to-rule-them-all.html – Qsiris Feb 17 '19 at 19:55