0

I'm currently running into some configuration problems with NGINX where I keep getting a 502 error instead of NGINX falling back onto a different directory if either the server is down or the directory doesn't exist.

I'm running a Node.js application on port :3000, have SSL set up, and have all HTTP requests redirect to HTTPS. Given the scenario where my node.js application is offline, I wish to send the client to the default NGINX root directory /usr/share/nginx/html index index.htm index.html if possible.

I'm trying to have the nodejs application on port 3000 be shown on / but in the case that the server is down, to fallback on NGINX default directory and display the index.html in there instead. Can anyone help or guide me through this process?

Thank you

Edit: I've tried jfriend00 said in the comments, but now my proxy_pass doesn't seem to work. It would now default to the 500.html regardless whether my server is running or not. I've attached my nginx.conf file, would appreciate any help.

events {
    worker_connections  1024;
}

http {

    upstream nodejs {
           server <<INTERNAL-PRIVATE-IP>>:3000; #3000 is the default port 
    }

    ...

    server {
        listen 80;
        server_name <<PUBLIC-IP>>;
        return 301 $scheme://<<DOMAIN>>$request_uri;
    }

    server {
        listen 443;
        ssl on;
                server_name <<DOMAIN>>.com www.<<DOMAIN>>.com;

                ...

                location / {
                    proxy_pass http://nodejs;
                    proxy_redirect off;
                    proxy_set_header Host $host ;
                    proxy_set_header X-Real-IP $remote_addr ;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
                    proxy_set_header X-Forwarded-Proto https;
            }

            error_page 501 502 503 /500.html;

            location = /500.html {
                root /usr/share/nginx/html;
            }

        }
}
Bryan
  • 1,438
  • 3
  • 15
  • 24
  • There's a similar question without an answer [http://stackoverflow.com/questions/15142692/varnish-nginx-node-js-static-nginx-html-pages-with-node-js-fallback](here). The other asker went through an unconventional approach. – Bryan Jul 22 '16 at 00:04
  • One way of doing it here: http://serverfault.com/questions/511109/nginx-local-fallback-error-page-if-proxy-destination-is-unavailable – jfriend00 Jul 22 '16 at 00:15
  • Hmm. I'll take a go at it, but at a glance, isn't it bad to put root inside the location block? @jfriend00 – Bryan Jul 22 '16 at 00:18
  • `location = xxx` requires an exact match so what is the issue with `root`? And, examples of `root` in the location here: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias – jfriend00 Jul 22 '16 at 00:24
  • @jfriend00 Tried what you did but now the proxy_pass doesn't seem to work, it would always go straight to the 500.html page. – Bryan Jul 22 '16 at 02:26

1 Answers1

0

Adding the error_page works as I did above and it successfully kicks back. Thanks @jfriend00.

If you're deploying it to a live server, you might want to check this out since I had a hard time trying to figure out why my proxy_pass and my NGINX configuration wasn't working on CentOS deployed on EC2. It had nothing to do with the error_page.

Community
  • 1
  • 1
Bryan
  • 1,438
  • 3
  • 15
  • 24