Nginx – custom 502 bad gataway for nginx, cannot get it to work, whats wrong

502-errorcustom-errorsnginxunicorn

Im trying to catch a 502 bad gataway for nginx with unicorn. This error is thrown when unicorn is not running. Im trying to use a custom error page instead for when unicorn is not running to no avail.

What did I miss? according to docs this should be right and I just don't see any errors.

upstream unicorn {
server unix:/srv/host/shared/tmp/unicorn.sock fail_timeout=0;
}

server {

    listen 80 deferred;
    server_name host.com host.com
    client_max_body_size 4G;
    keepalive_timeout 10;

    root /srv/host/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 nginx error.log shows:

 unix:/srv/host/shared/tmp/unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 83.117.60.95, server: host.com, request: "GET / HTTP/1.1", upstream: "http://unix:/srv/host/shared/tmp/unicorn.sock:/", host: "host.com"

Best Answer

Yes, you can have custom error-pages in nginx. Add something like the following to your config:

error_page  502 /path/to/error-pages/502_bad_gateway.html;