我有一台服务器,其中有两个两个使用节点http-server运行的单页应用程序(使用框架):
website1 running on port 80: IP_ADDRESS:80
website2 running on port 8080: IP_ADDRESS:8080
当前,工作流正在使用这两个命令来部署两个站点
pm2 start /usr/bin/http-server -f --name website1 -- -p 80 -d false
pm2 start /usr/bin/http-server -f --name website2 -- -p 8080 -d false
最后,我们的网站在这样的域上运行:
subdomain.mysite.com for website1
subdomain.mysite.com:8080 for website2.
这是不希望的,我们想要这样:
subdomain.mysite.com for website1
subdomain2.mysite.com for website2
我尝试使用以下反向代理配置安装nginx:
server {
listen 80;
server_name subdomain2.mysite.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
希望现在,如果我输入subdomain2.mysite.com,它将带我到端口8080上的website2,但没有,而是使我带到了端口80上的website1。实际上,我不确定该反向代理是否能正常工作。
我确定我配置错误,可能是什么问题?
p / s:我也想知道我是否做错了-如果我将nginx用于反向代理,是否应该停止直接使用http-server?
最佳答案
Node和Nginx不能同时侦听端口80。如果要反向代理,则都需要为第一个Node应用程序使用不同的端口,并将另一个虚拟主机添加到您的nginx文件中。
例如,将第一个应用程序的端口更改为8000看起来像:
pm2 start /usr/bin/http-server -f --name website1 -- -p 8000 -d false
pm2 start /usr/bin/http-server -f --name website2 -- -p 8080 -d false
使用以下Nginx配置:
server {
listen 80;
server_name subdomain2.mysite.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
server_name subdomain1.mysite.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
您可能还希望将
proxy_redirect off;
标志添加到每个块。
关于node.js - 当 Node http服务器运行时,nginx反向代理似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320278/