e x p r e s s i c a

ruby on rails, business and technicalities

No public Twitter messages.

  • RSS
  • Facebook
  • Twitter
  • Linkedin

Setting up custom error pages are needed whenever the app goes in maintenance or when the code gets crashed :P … In Nginx, its fairly simple but once when you know it.

Say your app example.com is residing in /var/www/apps/example

Nginx configurations

say your existing configuration looks like…

  server{
      listen 80;
      server_name .example.com;
      location / {
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect false;
           proxy_pass http://127.0.0.1:9002;
      }
  }

add the error page configurations

  server{
      listen 80;
      server_name .example.com;
      location / {
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect false;
           proxy_pass http://127.0.0.1:9002;
      }
      error_page 500 502 503 504  /500.html;
      location = /500.html {
          root  /var/www/maintenance/example;
      }
  }

Create the required html error page at /var/www/maintenance/example/500.html.
Now, restart the nginx to reload the configurations by issuing /etc/init.d/nginx restart.
And you are done!

7 Responses so far.

  1. Looks like one thing missing here,

    proxy_intercept_errors on;

    This is important for nginx to inspect response which is proxied….

    PS: more here http://wiki.nginx.org/NginxHttpProxyModule#proxy_intercept_errors

  2. [...] Check out the original for detail [...]

  3. nitrocarbon says:

    Nice. This was helpful!

  4. Pedro says:

    Hi Abhishek,

    That depends on your needs. In the case of my application, it handles 500 errors by itself. But when it isn’t running (in “maintenance mode”), nginx takes over and sends a proper 500 error page.

    Regards.

  5. I am having trouble getting my music to play this 404 page comes up saying not found. Please help me Thank you

  6. [...] Here is a excellent tutorial show you how to Set up error pages in Nginx: Setting up custom error pages are needed whenever the app goes in maintenance or when the code gets crashed … In Nginx, its fairly simple but once when you know it. [...]

  7. Rogergue says:

    How can I turn off all the default error pages and use 1 custom made?

    Thanks.


Sponsors