如何在nginx.conf中定义位置regex匹配块



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

html代码结构:

  • admin_view_groups.html展开布局.html(通过jinja2(
  • admin_view_groups.html,通过layout.html加载main.js

我期望基于";位置~/V1";nginx.conf中的块:
/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?"

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

  • 字符串V1/js/mlj/main.js匹配
  • 找不到文件/usr/src/app/web/V1/js/mlj/main.js
  • 找不到目录/usr/src/app/web/V1/js/mlj/main.js/
  • 找不到文件/index.html
  • 找不到文件index.php

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

  • 如何在字符串(例如admin_edit_group/V1/js/mlj/main.js(中匹配子字符串(例如,/V1(?(当regex模式位于字符串的中间时(
  • 当regex模式在字符串中间匹配时,为什么找不到文件

相关代码:

# 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需要捕获来构建文件的完整路径(请参阅本文档(

  • 在同一块中使用aliastry_files可能会导致奇怪的副作用(参见本文档(

  • try_files语句的末尾不需要同时使用/index.html/index.php。选择使用=404代码需要哪一个(请参阅本文档(

  • Nginx中的所有URI都以前导/开头-使用/index.php而不是"index.php">


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

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

最新更新