[英]How to configure nginx to redirect to same app from multiple ports?
I have a node.js app and I am running nginx as a reverse proxy server. 我有一个node.js应用程序,并且正在将nginx作为反向代理服务器运行。 I want to access the same app through multiple ways. 我想通过多种方式访问同一应用程序。
Let the ip address of machine be 12.16.12.113
. 设机器的IP地址为12.16.12.113
。 Then I want the same app to be accessible through: 然后,我希望可以通过以下方式访问同一应用:
12.16.12.113:3000
test.example.com
I have 2 Configuration files for nginx in sites-available
directory: 我在sites-available
目录中有2个nginx的配置文件:
file1: 文件1:
server {
listen 80;
server_name test.example.com;
location / {
proxy_pass http://localhost:3000;
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;
}
}
file2: 文件2:
server {
listen 3000;
server_name default_server;
location / {
proxy_pass http://localhost:3000;
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;
}
}
The problem is that if I link both the files in sites-enabled
directory, then only the app is accessible only through 12.16.12.113:3000
. 问题是,如果我同时链接了sites-enabled
目录中的两个文件,则只能通过12.16.12.113:3000
访问该应用程序。 If I remove file2 , then it is accessible through test.example.com
. 如果我删除file2 ,则可以通过test.example.com
访问它。
But I want both the methods to work. 但是我希望这两种方法都能起作用。
Edit 1: Based on suggestion from this answer, I did the following test: 编辑1:根据此答案的建议,我进行了以下测试:
It appears that nginx is not listening to port 80. Why is this happening? 看来nginx没有监听端口80。为什么会发生这种情况?
The are few ways to achieve that. 有几种方法可以做到这一点。
You can make your Node app listen on 12.16.12.113:3000 instead of on localhost:3000 and then you will need nginx only for port 80. 您可以使Node应用程序在12.16.12.113:3000上侦听,而不是在localhost:3000上侦听,然后仅需要80端口的nginx。
You can make your Node app listen on localhost:3000 (but only on localhost) and then proxy 12.16.12.113:3000 and port 80 by nginx. 您可以让Node应用程序在localhost:3000上侦听(但只能在localhost上),然后通过nginx代理12.16.12.113:3000和端口80。
You can make your Node app listen on localhost:4000 or 0.0.0.0:4000 on any interface and use nginx to proxy requests to port 80 and 3000. 您可以使Node应用程序在任何接口上的localhost:4000或0.0.0.0:4000上侦听,并使用nginx代理对端口80和3000的请求。
But you will not be able to make them both listen on the same port and the same interface without problems. 但是,您将无法使它们都在同一端口和同一接口上侦听而不会出现问题。
This is the only advice that could be given considering the fact that you didn't include any code example of how your Node app is binding to the ports, which would be the only relevant information here. 考虑到您没有包含任何有关Node app如何绑定到端口的代码示例的事实,这是可以给出的唯一建议,这将是此处唯一的相关信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.