custom 502 error page for nginx with unicorn not working, driving me insane ;)

Rubytastic asked:

I tried several times and on multiple occasions to try and fix the below code for nginx with unicorn, but it just isen’t working. So now posting here in last report some of you more talented could see my mistake and others can benefit from it too.

Im using:

  • nginx
  • unicorn

The problem is

  • No 502 error shown ( I use cap deploy:web:disable task to write the maintenance.html )
  • If nginx is running but unicorn is not im also getting a 502 error page

The nginx config:

upstream unicorn {

        server unix:/srv/books/shared/tmp/unicorn.sock fail_timeout=0;


    server {

        listen 80 deferred;
        server_name        books.*;

            client_max_body_size 4G;
            keepalive_timeout 10;
            server_tokens off;

            root /srv/books/public;

            location / {
                try_files /system/maintenance.html $uri/index.html $uri @unicorn;

            try_files $uri/index.html $uri @unicorn;

            location @unicorn {
                error_page 502 /system/maintenance.html;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://unicorn;


My answer:

Place the error_page directive under server rather than location.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.