【问题标题】:nginx with multiple locations directives with subdomains具有多个位置指令和子域的 nginx
【发布时间】:2021-11-08 07:46:13
【问题描述】:

我正在尝试在 nginx conf 中实现类似的东西:

子域

  • sub.domain.com -> 服务 html
  • sub.domain.com/api -> 代理到端口 3001
  • sub.domain.com/viewer -> 提供另一个 html

子域2

  • sub2.domain.com -> 代理到端口 3000

唯一不起作用的路径是查看器,我从“位置/”获取html。所有其他配置都运行良好。

我尝试将查看器移到底部,然后移到顶部和中间,不管它不起作用。

我使用 CentOS7。这是服务器中当前的配置:

events {
}
http {
    server {
            listen 80;
            listen [::]:80;
            server_name www.sub.domain.com subdomain.com;

            location /viewer {
                    root /opt/viewer/;
                    try_files $uri /index.html;
                    index index.html;
            }

            location / {
                    root /opt/client-bo/;
                    try_files $uri /index.html;
                    index index.html;
            }

            location /api {
                    proxy_pass "http://localhost:3001";
            }
    }
    server {
            listen 80;
            server_name www.sub2.domain.com sub2.domain.com;
            listen [::]:80;
            location / {
                    proxy_pass "http://localhost:3000";
            }
    }
}

谢谢!

【问题讨论】:

    标签: nginx centos7


    【解决方案1】:

    如果您的查看器应用程序位于/opt/viewer 目录中,并且您希望它在/viewer URI 前缀下可用,您应该使用root /opt; 来表示location /viewer { ... }。检查rootalias 指令之间的区别。

    接下来,try_files 指令的最后一个参数被视为要重新评估的新 URI,因此您的 /index.html 被视为将与 location / { ... } 一起提供的新 URI。您应该将该指令更改为

    try_files $uri /viewer/index.html;
    

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      • 2012-11-09
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多