Nginx

NGINX:位置匹配資產

  • September 10, 2022

我的位置與一台伺服器應該提供的資源匹配時遇到問題,因為它具有某種格式作為目錄

使用此配置:

location ~ "/web/" {
   set $upstream_host http://webServer;
   set $upstream_port 1234;
   set $upstream_server "${upstream_host}:$upstream_port";

   proxy_pass $upstream_server;

   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Host $host;
   proxy_set_header X-Forwarded-Proto $scheme;

   proxy_redirect off;
}

location / {
   proxy_pass http://staticHomePageServer;

   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Host $host;
   proxy_set_header X-Forwarded-Proto $scheme;

   proxy_redirect off;
}

作品 - https://my.example.com/web/

作品 - https://my.example.com/someOtherSub/web.jpg

不起作用 - https://my.example.com/web (404)

如果我改變

location ~ "/web/" {

location ~ "/web" {

然後子目錄重定向工作,但我在 jpg 上得到 404

你試過錨定你的正則表達式嗎?

location ~ "^/web" {

引用自:https://serverfault.com/questions/1110374