带有子域的多个位置的Nginx指令 [英] nginx with multiple locations directives with subdomains

查看:24
本文介绍了带有子域的多个位置的Nginx指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

子域

  • sub.domain.com->;服务器html
  • sub.domain.com/api->;端口3001的代理
  • sub.domain.com/view->;为另一个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";
            }
    }
}

谢谢!

推荐答案

如果您的查看器应用程序位于/opt/viewer目录中,并且您希望它在/viewerURI前缀下可用,则应使用root /opt;作为location /viewer { ... }。检查rootalias指令之间的差异。

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

try_files $uri /viewer/index.html;

这篇关于带有子域的多个位置的Nginx指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆