nginx not serving my error_page

NginxCustom Error-Pages

Nginx Problem Overview


I have a Sinatra application hosted with Unicorn, and nginx in front of it. When the Sinatra application errors out (returns 500), I'd like to serve a static page, rather than the default "Internal Server Error". I have the following nginx configuration:

server {
  listen 80 default;
  server_name *.example.com;
  root /home/deploy/www-frontend/current/public;

  location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 5;
    proxy_read_timeout 240;
    proxy_pass http://127.0.0.1:4701/;
  }

  error_page 500 502 503 504 /50x.html;
}

The error_page directive is there, and I have sudo'd as www-data (Ubuntu) and verified I can cat the file, thus it's not a permission problem. With the above config file, and service nginx reload, the page I receive on error is still the same "Internal Server Error".

What's my error?

Nginx Solutions


Solution 1 - Nginx

error_page handles errors that are generated by nginx. By default, nginx will return whatever the proxy server returns regardless of http status code.

What you're looking for is proxy_intercept_errors

> This directive decides if nginx will intercept responses with HTTP > status codes of 400 and higher. > > By default all responses will be sent as-is from the proxied server. > > If you set this to on then nginx will intercept status codes that are > explicitly handled by an error_page directive. Responses with status > codes that do not match an error_page directive will be sent as-is > from the proxied server.

Solution 2 - Nginx

You can set proxy_intercept_errors especially for that location

location /some/location {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 5;
    proxy_read_timeout 240;
    proxy_pass http://127.0.0.1:4701/;
    proxy_intercept_errors on; # see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors

    error_page 400 500 404 ... other statuses ... =200 /your/path/for/custom/errors;
}

and you can set instead 200 other status what you need

Solution 3 - Nginx

People who are using FastCGI as their upstream need this parameter turned on

fastcgi_intercept_errors on;

For my PHP application, I am using it in my upstream configuration block

 location ~ .php$ { ## Execute PHP scripts
    fastcgi_pass   php-upstream; 
    fastcgi_intercept_errors on;
    error_page 500 /500.html;
 }

Solution 4 - Nginx

As mentioned by Stephen in this response, using proxy_intercept_errors on; can work. Though in my case, as seen in this answer, using uwsgi_intercept_errors on; did the trick...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrançois BeausoleilView Question on Stackoverflow
Solution 1 - NginxStephen EmslieView Answer on Stackoverflow
Solution 2 - NginxAlexeyView Answer on Stackoverflow
Solution 3 - NginxAftab NaveedView Answer on Stackoverflow
Solution 4 - NginxppythonView Answer on Stackoverflow