"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in D:\nginx-1.24.0/conf/nginx.conf:106
使用正则匹配请求,如果代理中携带 /+其它字符串,将会报错:
server {
listen 7777;
# 匹配所有 xxx 的静态资源请求
location ~ /xxx/(.*\.\w+) {
proxy_pass http://127.0.0.1:6666;
}
# 匹配所有 yyy 静态资源请求
location ~ /(.*\.\w+) {
proxy_pass http://127.0.0.1:6666/yyy/; # 报错
}
# 匹配所有 不带静态资源后缀 的请求
location / {
proxy_pass http://127.0.0.1:9999;
}
解决方法:
在 location 中加上 $1,正常启动:
server {
listen 7777;
# 匹配所有 xxx 的静态资源请求
location ~ /xxx/(.*\.\w+) {
proxy_pass http://127.0.0.1:6666;
}
# 匹配所有 yyy 静态资源请求
location ~ /(.*\.\w+) {
proxy_pass http://127.0.0.1:6666/yyy/$1; # 不报错
}
# 匹配所有 不带静态资源后缀 的请求
location / {
proxy_pass http://127.0.0.1:9999;
}
时间问题,未深入了解,猜测可能是 nginx 匹配到正则之后,如果 URI 中又带上字符串后缀,nginx 就不知道把匹配的 URL 做何处理,索性直接抛错,因此如果使用正则匹配 location 则强制要求:
1、proxy_pass 只写域名;
2、proxy_pass 加上自定义字符串,但必须手动把匹配结果添加正则匹配结果;
如果有谁看过文档,知道原因,能解答疑惑非常感谢