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's error_page directive allows you to redirect users to a defined page or resource or URL when an error occurs. It also optionally allows for modification of the HTTP status code in the response to a client.
Creating Your Custom Error Pages Put your custom error pages in the /usr/share/nginx/html directory where Nginx sets its default document root. You'll make a page for 404 errors called custom_404. html and one for general 500-level errors called custom_50x.
Passing Request HeadersBy default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings.
NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With