Custom Error Pages With Nginx and Thin
If you are using Nginx to proxy to another web server such as Thin or Mongrel, chances are you have had trouble getting your custom error pages to return the correct status codes.
If your application redirects to a cached error page Nginx will serve it as a normal file and will (correctly) tell the browser that everything was successful. Not quite the intended behaviour.
To fix this, add locations for your error pages and set them to be internal. Now the locations can only be reached when somethig goes wrong (as intended) and Nginx will never mistakenly serve them as valid files.
upstream thins {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
access_log /var/www/example/log/access.log;
error_log /var/www/example/log/error.log;
root /var/www/example/public/;
server {
listen 80;
server_name www.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
error_page 404 /404.html;
error_page 502 503 /503.html;
location ~* ^/(404|502|503).html$ {
if (!-f $request_filename) {
proxy_pass http://thins; break;
}
internal; break;
}
location / {
if (!-f $request_filename) {
proxy_pass http://thins;
break;
}
}
}