Search code examples
nginxwebserveruwsgiwsgi

nginx is not showing custom error page with uwsgi


I'm trying to add a custom error page for 502 and 504 answers. Currently I have the following nginx configuration for my site (other configuration files are untouched):

server {
    listen       80;
    listen       [::]:80;

    server_name 10.12.112.163;

    add_header X-UA-Compatible "IE=Edge,chrome=1";
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    charset utf-8;
    try_files $uri @icons;

    error_page 502 504 /502.html;
    location = /502.html {
       root /home/dmoj/site;
       internal;
    }

    location @icons {
        root /home/dmoj/site/resources/icons;
        error_page 403 404 = @uwsgi;
        log_not_found off;
    }

    location @uwsgi {
        uwsgi_read_timeout 600;
        uwsgi_pass unix:///tmp/dmoj-site.sock;
        include uwsgi_params;
    }
}

When I have uwsgi stopped and take my browser to 10.12.112.163 the default '502 Bad Gateway' message of nginx is showed, however when I go to 10.12.112.163/502.html my page displays perfectly.

I also tested it without the internal directive and it is the same.


Solution

  • Not sure if it's documented somewhere, but I don't think error_page can be chained to another error_page.

    In your case, the error_page 404 is very similar to the behaviour of the try_files statement. See this document for details.

    So your chained error_page can be replace, like this:

    location @icons {
        root /home/dmoj/site/resources/icons;
        try_files $uri @uwsgi;
        log_not_found off;
    }