Nginx 服务器静态文件与 try_files 位置下

Nginx 服务器静态文件与 try_files 位置下

我有以下 nginx 配置:

server {

    listen 80;

    charset     utf-8;
    client_max_body_size 5M;

    location /severbat {

        root /root/severbat/dist;
        try_files $uri /index.html;
    }

    location /swagger {
        root /root/av/swagger-ui-dist;
        try_files $uri /index.html;
    }

}

我文件/root/severbat/dist/index.html/root/av/swagger-ui-dist/index.html存在。但如果我打开 urlhttp://本地主机/severbat我看到的是 nginx 问候页面,而不是 index.html

但是如果我提供以下文件/有用:

location / {
    root /root/severbat/dist;
    try_files $uri /index.html;
}

nginx-访问日志

2018/10/03 09:32:44 [debug] 23052#23052: *519 trying to use file: "/swagger" "/root/av/swagger-ui-dist/swagger"
2018/10/03 09:32:44 [debug] 23052#23052: *519 trying to use file: "/index.html" "/root/av/swagger-ui-dist/index.html"
2018/10/03 09:32:44 [debug] 23052#23052: *519 internal redirect: "/index.html?"
2018/10/03 09:32:44 [debug] 23052#23052: *519 rewrite phase: 1
2018/10/03 09:32:44 [debug] 23052#23052: *519 test location: "/severbat"
2018/10/03 09:32:44 [debug] 23052#23052: *519 test location: "/api"
2018/10/03 09:32:44 [debug] 23052#23052: *519 using configuration ""
2018/10/03 09:32:44 [debug] 23052#23052: *519 http cl:-1 max:5242880
...
2018/10/03 09:32:44 [debug] 23052#23052: *519 content phase: 17
2018/10/03 09:32:44 [debug] 23052#23052: *519 http filename: "/usr/share/nginx/html/index.html"

答案1

您好,欢迎来到 ServerFault。

我认为你的问题是 NGINX附加location与指定的匹配root,因此当您输入时,例如,http://localhost/swaggerNGINX 会查找目录/root/av/swagger-ui-dist/swagger但找不到它。

这就是该root指令在 NGINX 中的工作方式

您可能希望改用该alias指令:

location /severbat {
    alias /root/severbat/dist;
    try_files $uri $uri/ index.html;
}

location /swagger {
    alias /root/av/swagger-ui-dist;
    try_files $uri $uri/ index.html;
}

相关内容