【问题标题】:How to configure nginx to use same port with same server name for 2 sites如何配置 nginx 为 2 个站点使用具有相同服务器名称的相同端口
【发布时间】:2020-04-21 12:53:23
【问题描述】:

我在一台 debian 机器上有 2 个站点。目前一个在端口 443/80 上运行,另一个在端口 8090 上运行。我的要求是在 443 上运行这两个站点,如果我进入 site.com/1 - 我应该被重定向到站点 1,如果我进入站点。 com/2 - 我应该到达站点 2。如何使用 nginx 实现这一点?

我尝试过代理,但问题是 - 如果我为两个监听使用相同的端口,它会忽略已在使用中的声明。

【问题讨论】:

    标签: nginx nginx-location nginx-config


    【解决方案1】:

    根据您的要求,您希望运行两个具有相同域的站点。让我们说

    example.com/site1
    example.com/site2
    

    为此,您需要按照以下模板修改 nginx 配置文件 打开与 example.com 域关联的 nginx 配置文件并添加以下代码。

        server {
          listen 80;
          server_name  example.com;
          return 301 https://example.com$request_uri;
        }
    
        server {
          listen 443 ssl http2;
          server_name example.com; 
          modsecurity_transaction_id "example.com-$request_id"; 
          access_log           /var/log/nginx/access.log; 
          error_log            /var/log/nginx/error.log; 
          include              /etc/nginx/default.d/example.com/*.conf;
    location / {
                try_files $uri $uri/ =404;
        }
    
    
        location /site1 {
                index index.php index.html index.htm;
                try_files $uri $uri/ /site1/index.php?q=$uri&$args;
        }
    
        location /site2 {
                index index.php index.html index.htm;
                try_files $uri $uri/ /site2/index.php?q=$uri&$args;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多