如何在 nginx.conf 中定义位置正则表达式匹配块

How to define a location regex matching block in nginx.conf

我有一个带有 nginx 的网络应用程序(在 docker 容器中:webserver_nginx_1),我正在尝试配置它。

.html代码结构:

我期待以下映射,基于 nginx.conf 中的“location ~ /V1”块:
/V1/js/mlj/main.js -> /usr/src/app/web/V1/js/mlj/main.js

# the file exists within the container  
docker  exec  -it webserver_nginx_1 bash
root@90c800c4bd28:/# ls -l /usr/src/app/web/V1/js/mlj/main.js
-rwxrwxrwx 1 1000 1000 1709 Jan  9 06:49 /usr/src/app/web/V1/js/mlj/main.js

但是文件没有加载,我看到以下错误:

# In chrome devtools
GET https://localhost/admin_edit_group/V1/js/mlj/main.js net::ERR_ABORTED 404
   
# In the log file for the nginx docker container I get a 404 error
# docker logs -f webserver_nginx_1
...
172.28.0.1 - - [10/Jan/2021:06:20:21 +0000] "GET /admin_edit_group/V1/js/mlj/main.js HTTP/2.0" 404 548 "https://localhost/admin_edit_group/2" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" "-"
172.28.0.1 - - [10/Jan/2021:06:20:21 +0000] "GET /admin_edit_group/V1/js/mlj/main.js HTTP/2.0" 404 548 "https://localhost/admin_edit_group/2" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" "-"
2021/01/10 06:20:21 [error] 7#7: *14 open() "/etc/nginx/htmlindex.php" failed (2: No such file or directory), client: 172.28.0.1, server: localhost, request: "GET /admin_edit_group/V1/js/mlj/main.js HTTP/2.0", host: "localhost", referrer: "https://localhost/admin_edit_group/2"

“V1”位置块的日志:

tail -f /var/log/nginx/V1-error.log
2021/01/10 06:34:31 [debug] 9#9: *2 http script copy: "/usr/src/app/web/V1"
2021/01/10 06:34:31 [debug] 9#9: *2 http script var: "/V1/js/mlj/main.js"
2021/01/10 06:34:31 [debug] 9#9: *2 trying to use file: "/V1/js/mlj/main.js" "/usr/src/app/web/V1/V1/js/mlj/main.js"
2021/01/10 06:34:31 [debug] 9#9: *2 http script var: "/V1/js/mlj/main.js"
2021/01/10 06:34:31 [debug] 9#9: *2 trying to use dir: "/V1/js/mlj/main.js" "/usr/src/app/web/V1/V1/js/mlj/main.js"
2021/01/10 06:34:31 [debug] 9#9: *2 trying to use file: "/index.html" "/usr/src/app/web/V1/index.html"
2021/01/10 06:34:31 [debug] 9#9: *2 trying to use file: "index.php" "/usr/src/app/web/V1index.php"
2021/01/10 06:34:31 [debug] 9#9: *2 internal redirect: "index.php?"

它看起来像在位置匹配块中:

我的问题是:
在 nginx 中,在一个位置块内:


相关代码:

# the html files:
cat admin_view_groups.html
...
{% extends "layout.html" %}

# --------------------------------------------------------------

cat layout.html
...
<script type="module" src="V1/js/mlj/main.js"></script>

# --------------------------------------------------------------

# the location regex block in the nginx conf file
cat nginx.conf
...

    location ~ /V1 {
        error_log /var/log/nginx/V1-error.log info; 
        autoindex on;
        alias /usr/src/app/web/V1;
        try_files $uri $uri/ /index.html index.php;
    }

您的 location 块有几个问题。

  • 在正则表达式 location 中使用 alias 需要捕获以构建文件的完整路径(参见 this document

  • 在同一个块中使用 aliastry_files 会导致奇怪的副作用(参见 this document

  • 您不需要在 try_files 语句末尾同时使用 /index.html/index.php。选择需要哪一个才能使用 =404 代码(参见 this document

  • Nginx 中的所有 URI 都以 / 开头 - 使用 /index.php 而不是 `index.php'


要匹配任何包含嵌入式 /V1/ 的 URI,您可以使用:

location ~ (/V1.*)$ {
    root /usr/src/app/web;
    try_files  =404;
}