【问题标题】:nginx subdomain not redirecting to desired portnginx 子域未重定向到所需端口
【发布时间】:2018-11-23 17:53:03
【问题描述】:

我有一个要求,我需要将子域重定向到管理面板和 域到网站的正面部分。我在 Godaddy 中创建了 subdoamin 和 在谷歌计算引擎上使用 nginx。
这是我正在使用的 nginx conf 设置:

子域规则

server {
   listen 80;
   listen [::]:80;

   server_name admin.example.com;

   root /var/www/html;
   index index.html;


   location / {
       proxy_pass http://127.0.0.1:8000;
   }
}

主站点规则

server {
   listen 80;
   listen [::]:80;


   server_name example.com;

   root /var/www/html;
   index index.html;


   location / {
           try_files $uri $uri/ =404;
   }
}

子域规则和主站点规则都在文件/etc/nginx/nginx.conf

问题是,“example.com”和“admin.example.com”都转到“example.com”。

我想,我做了很多谷歌搜索,但没有得到解决方案。

提前致谢。

【问题讨论】:

    标签: nginx reverse-proxy


    【解决方案1】:

    您的子域和主站点规则相同。

    root /var/www/html;
    index index.html;
    

    你可以通过制作另一个目录和子域来解决这个问题:

    root /var/www/site2/html;
    index index.html
    

    【讨论】:

    • 但是 server_name 不同,对于子域我使用 proxy_pass 重定向,为什么会使用 root 设置?我仍然通过更改它来检查它,它没有工作。
    • 如果您通过代理传递重定向,您必须删除子域配置中的根和索引行。
    • 你是否更改了子域配置
    • 清除浏览器缓存,然后重试。
    【解决方案2】:

    使您的子域配置文件看起来像:

    server {
        listen 80;
        server_name admin.example.com;
    
        location / {
            proxy_pass http://127.0.0.1:8080;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2014-05-17
      • 2015-01-04
      • 2013-05-09
      • 2020-12-30
      • 2017-04-04
      • 2016-03-21
      • 2020-12-07
      • 1970-01-01
      相关资源
      最近更新 更多