nginx 测试正确的别名文件,然后从错误的位置提供文件

nginx 测试正确的别名文件,然后从错误的位置提供文件

我有这个服务器块:

server {
    listen 80;
    server_name admin.app;
    root /opt/project/public/src/;

    charset utf-8;

    location ~ ^/dashboard/?(.*)$ {
       alias /opt/project/dashboard/src;
       try_files /$1 /testindex.html;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/project.app-error.log debug;
    rewrite_log on;

}

并且这个目录结构:

/opt/project/dashboard/src/
    test.svg
    testindex.html

当我去的时候http://localhost/dashboard/test.svg它工作正常,但如果我去http://localhost/dashboard/testing我想让它服务/opt/project/dashboard/src/,但调试却显示这一点:

2017/06/16 19:36:11 [debug] 385#0: *46 http request line: "GET /dashboard/testing HTTP/1.1"
2017/06/16 19:36:11 [debug] 385#0: *46 http uri: "/dashboard/testing"
2017/06/16 19:36:11 [debug] 385#0: *46 http script copy: "/opt/project/dashboard/src/"
2017/06/16 19:36:11 [debug] 385#0: *46 http script capture: "testing"
2017/06/16 19:36:11 [debug] 385#0: *46 trying to use file: "testing" "/opt/project/dashboard/src/testing"
2017/06/16 19:36:11 [debug] 385#0: *46 trying to use file: "testindex.html" "/opt/project/dashboard/src/testindex.html"
2017/06/16 19:36:11 [debug] 385#0: *46 internal redirect: "testindex.html?"
2017/06/16 19:36:11 [debug] 385#0: *46 rewrite phase: 1
2017/06/16 19:36:11 [debug] 385#0: *46 test location: "/robots.txt"
2017/06/16 19:36:11 [debug] 385#0: *46 test location: ~ "^/dashboard/?(.*)$"
2017/06/16 19:36:11 [debug] 385#0: *46 using configuration ""
2017/06/16 19:36:11 [debug] 385#0: *46 http filename: "/opt/project/public/src/testindex.html"

(仅为简洁起见,仅显示日志的相关部分)

并且它给出了 404(或者如果我将它放入 ,它将提供错误的文件public/src)。它显示它测试了现有的 testindex.html 文件,但随后关闭重定向并尝试从public/src目录提供它。我不希望它从public/src该路径提供任何服务。为什么它不会发送testindex.htmldashboard/src我已经为此苦苦挣扎了几个小时,尝试了许多不同的配置。

编辑:我将 try_files 更改为:try_files /$1 /dashboard/testindex.html;。结果如下:

2017/06/16 20:04:08 [debug] 464#0: *65 trying to use file: "/dashboard/testindex.html" "/opt/project/dashboard/src/dashboard/testindex.html"
2017/06/16 20:04:08 [debug] 464#0: *65 internal redirect: "/dashboard/testindex.html?"
2017/06/16 20:04:08 [debug] 464#0: *65 rewrite phase: 1
2017/06/16 20:04:08 [debug] 464#0: *65 test location: ~ "^/dashboard/?(.*)$"
2017/06/16 20:04:08 [debug] 464#0: *65 using configuration "^/dashboard/?(.*)$"
2017/06/16 20:04:08 [debug] 464#0: *65 try files phase: 11
2017/06/16 20:04:08 [debug] 464#0: *65 http script copy: "/opt/project/dashboard/src"
2017/06/16 20:04:08 [debug] 464#0: *65 http script copy: "/"
2017/06/16 20:04:08 [debug] 464#0: *65 http script capture: "testindex.html"
2017/06/16 20:04:08 [debug] 464#0: *65 trying to use file: "/testindex.html" "/opt/project/dashboard/src/testindex.html"
2017/06/16 20:04:08 [debug] 464#0: *65 try file uri: "/testindex.html"
2017/06/16 20:04:08 [debug] 464#0: *65 http script copy: "/opt/project/dashboard/src"
2017/06/16 20:04:08 [debug] 464#0: *65 http filename: "/opt/project/dashboard/src/testindex.html"

它似乎正在工作,但我不明白发生了什么,它仍然在进行重定向,但它正在寻找错误的路径,所以不确定它是如何正确的或者发生了什么。@confused

相关内容