如何在443上运行Node.js服务器以确保Nginx不会停止工作 [英] How to run nodejs server over 443 ensuring nginx doesnt stop working
问题描述
我的Nginx默认文件如下:
My Nginx default file looks like this:
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停止工作.
Now, Nginx is running properly but when I try to run my nodejs server on port 443(https) it says EADDR already in use. When I kill the port to use my nodejs server instead, it also kills Nginx and Nginx stops working.
如何在443上运行我的nodejs服务器,以确保nginx不会关闭.
How do I run my nodejs server on 443 ensuring nginx doesn't close.
推荐答案
您不能在端口443和nginx上同时运行nodejs来同时服务ssl(443).您可以通过将nginx配置为nodejs的反向代理来实现.
You can't run nodejs on port 443 and nginx to serve ssl(443) at the same time. You can do this by configuring nginx as a reverse proxy for nodejs.
假设您正在端口3000中运行nodejs.
Let say you are running nodejs in port 3000.
const http = require('http');
http.createServer((req,res) => {
res.writeHead(200, {"Content-Type":"plain/html"});
res.end('Node is Running');
}).listen(3000);
您的nginx配置应为:
your nginx config should be:
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;
}
}
希望有帮助.
这篇关于如何在443上运行Node.js服务器以确保Nginx不会停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!