简体   繁体   English

Nginx使用Node.js进行后备

[英]Nginx fallback with Node.js

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. 我目前遇到NGINX的一些配置问题,如果服务器关闭或目录不存在,我会不断收到502错误,而不是NGINX退回到另一个目录。

I'm running a Node.js application on port :3000, have SSL set up, and have all HTTP requests redirect to HTTPS. 我在端口:3000上运行Node.js应用程序,设置了SSL,并将所有HTTP请求重定向到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. 给定我的node.js应用程序处于脱机状态的情况,如果可能的话,我希望将客户端发送到默认的NGINX根目录/usr/share/nginx/html index index.htm index.html

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. 我试图在/上显示端口3000上的nodejs应用程序,但在服务器关闭的情况下,回退到NGINX默认目录并在其中显示index.html。 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. 编辑 :我已经尝试了jfriend00在评论中说,但现在我的proxy_pass似乎不起作用。 It would now default to the 500.html regardless whether my server is running or not. 现在无论我的服务器是否运行,它都默认为500.html。 I've attached my nginx.conf file, would appreciate any help. 我已经附上了我的nginx.conf文件,不胜感激。

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;
            }

        }
}

Adding the error_page works as I did above and it successfully kicks back. 添加error_page的方法与上述操作相同,并且成功地将其踢回。 Thanks @jfriend00. 谢谢@ 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. 如果要将其部署到实时服务器上,则可能需要检查一下 ,因为我很难理解为什么我的proxy_pass和NGINX配置不能在EC2上部署的CentOS上运行。 It had nothing to do with the error_page. 它与error_page无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM