【问题标题】:Run multiple web apps with single nginx server使用单个 nginx 服务器运行多个 Web 应用程序
【发布时间】:2021-09-22 16:38:12
【问题描述】:

我有 3 个使用 express 模块用 node js 编写的应用程序。每个应用程序都支持 http 和 https。我为三个不同的端口创建了它。我想在具有单个 IP 但不同域的单个 pc 上运行所有应用程序。我有一个linux服务器。另外,我听说 nginx 可能可以做到这一点。我想像下面的例子

Localhost:3000 -> www.domain.com
Localhost:3001 ->subdomain.domain.com
Localhost:3002 -> www.anotherdomain.com

如何在 linux 环境下使用它?请指导我使用合适的例子。

【问题讨论】:

  • 我投票结束这个问题,因为它不是一个编程问题,而且它在 Stack Overflow 上是题外话。有关您网站的非编程问题应在Webmasters 上提出,有关管理服务器的问题应在Server Fault 上提出。以后,请在这两个地方之一提出这样的问题。

标签: javascript node.js linux nginx server


【解决方案1】:
  1. 首先安装NGINXhttps://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/
  2. /etc/nginx/这样的路径下编辑nginx的配置文件

先学会编辑配置文件对你有好处。 https://docs.nginx.com/nginx/admin-guide/web-server/web-server/

  1. 以下是一些示例:
server {
    listen 80;
    server_name www.domain.com;

    location / {
        proxy_pass http://Localhost:3000;
    }
}

server {
    listen 80;
    server_name subdomain.domain.com;

    location / {
        proxy_pass http://Localhost:3001;
    }
}

server {
    listen 80;
    server_name www.anotherdomain.com;

    location / {
        proxy_pass http://Localhost:3002;
    }
}

当您使用域访问您的页面时,上述配置可能会对您有所帮助。 更重要的是,当您使用 ip 地址访问时,您应该指定一台服务器作为您的默认服务器。

【讨论】:

    【解决方案2】:

    假设你已经安装了 Nginx (apt install Nginx)。

    只需为每台服务器创建一个单独的虚拟主机文件。

    比如www.domain.com可以是这样的

    /etc/nginx/sites-enabled/domain.com.conf
    
    server {
            listen 80;
            server_name www.domain.com;
    
            
            location / {
              proxy_pass localhost:3000;
            }
    }
    

    和 subdomain.domain.com 一样

    /etc/nginx/sites-enabled/subdomain.domain.com.conf
    
    server {
            listen 80;
            server_name subdomain.domain.com;
    
            
            location / {
              proxy_pass localhost:3001;
            }
    }
    

    我避免使用 SSL、根文件等,因为这可以在其他地方找到。

    https://hackprogramming.com/how-to-setup-subdomain-or-host-multiple-domains-using-nginx-in-linux-server/

    不要忘记测试 nginx sudo nginx -t 并重新启动它以获取更改 (sudo service nginx restart)

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多