如何在 443 上运行 Nodejs 服务器,确保 nginx 不会停止工作

2023-11-30

我的 Nginx 默认文件如下所示:

server {
       listen  80;
       server_name humanfox.com www.humanfox.com;
       rewrite  ^/(.*)$ https://www.humanfox.com$1 permanent;
}


server {
        listen 443 ssl spdy;
        server_name humanfox.com www.humanfox.com;
        ssl on;
        ssl_certificate /root/ca3/www.humanfox.com.crt;
        ssl_certificate_key /root/ca3/humanfox.com.key;
        access_log  /var/log/nginx/humanfox.com.access.log;
        error_log   /var/log/nginx/humanfox.com.error.log;
        rewrite     ^/(.*)$ https://www.humanfox.com$1 permanent;
}

现在,Nginx 运行正常,但是当我尝试在端口 443(https) 上运行我的 Nodejs 服务器时,它说 EADDR 已在使用中。 当我终止端口以使用我的 Nodejs 服务器时,它也会终止 Nginx 并且 Nginx 停止工作。

如何在 443 上运行我的 NodeJS 服务器,确保 nginx 不会关闭。


您不能在端口 443 上运行 nodejs 和 nginx 来同时提供 ssl(443) 服务。您可以通过将 nginx 配置为 Nodejs 的反向代理来实现此目的。

假设您正在端口 3000 上运行 Nodejs。

const http = require('http');
http.createServer((req,res) => {
  res.writeHead(200, {"Content-Type":"plain/html"});
  res.end('Node is Running');
}).listen(3000);

你的 nginx 配置应该是:

server {
    listen 443 ssl spdy;

    server_name humanfox.com www.humanfox.com;
    ssl on;
    ssl_certificate /root/ca3/www.humanfox.com.crt;
    ssl_certificate_key /root/ca3/humanfox.com.key;
    access_log  /var/log/nginx/humanfox.com.access.log;
    error_log   /var/log/nginx/humanfox.com.error.log;

    location / {
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
    }
}

希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 443 上运行 Nodejs 服务器,确保 nginx 不会停止工作 的相关文章

随机推荐