Custom Nginx error pages on upstream responses

Digital Ocean has a great post on setting up error pages when nginx encounters an error. However, the above does not work when a request is successfully passed to an upstream server and the upstream response is a 50x error. When processing the upstream response is desired, proxy_intercept_errors on; must be added to the configuration (thank you Stack Overflow!).

The server block looks like this:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        [...]

        proxy_intercept_errors on;
        error_page 500 502 503 504 /custom_50x.html;
        location = /custom_50x.html {
                root /usr/share/nginx/html;
                internal;
        }

        [...]
}
Advertisements

Django and nginx

Using Django 1.0 and above with nginx requires care when setting up the FCGI parameters. If SCRIPT_NAME ('') and PATH_INFO ($uri) are not properly set, URLs will not work properly.

Below is an example, which redirects requests to a Django socket.

    location /
    {
        fastcgi_read_timeout  5m;
        include fastcgi_params;

        fastcgi_param   SCRIPT_NAME       '';
        fastcgi_param   PATH_INFO         $uri;

        if (!-f $request_filename) {
            fastcgi_pass  unix:/var/www/website/socket;
            break;
        }
    }

References

problem with URLs on FastCGI after update to 1.0