简体   繁体   English

如何配置nginx从多个端口重定向到同一应用程序?

[英]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: 然后,我希望可以通过以下方式访问同一应用:

  1. 12.16.12.113:3000
  2. 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. 有几种方法可以做到这一点。

  1. 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。

  2. 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。

  3. 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.

相关问题 多个端口上的节点 nginx 代理,同一个域不起作用 - node nginx proxy on multiple ports, same domain not working 如何在Apache上配置端口? - How to configure ports on Apache? 如何在单个nginx安装中配置多个域以使用同一应用程序实例? - How to configure multiple domains, within a single nginx installation to use the same instance of an application? 如何将多个子域重定向到同一个正在运行的 Express 应用程序 - How to redirect multiple subdomains to the same running express app 将 Nginx 与多个节点应用程序一起使用到同一服务器 - Using Nginx with multiple node app into same server 如何将Nginx从http配置为https - How to configure nginx from http to https 在WebStorm中配置端口 - React + Node Express应用程序 - Configure ports in WebStorm - React + Node Express app 使用PM2,如何将node.js应用程序部署到同一服务器上的多个环境和端口? - Using PM2, how can I deploy my node.js app to multiple environments and ports on the same server? 针对Nodejs中多个端口的ElasticBeanStalk NGinx配置 - ElasticBeanStalk NGinx configuration for multiple ports in Nodejs 如何在 docker 上公开同一个 nodejs 应用程序的两个端口 - How can I expose two ports of the same nodejs app on docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM