Nginx 位置/覆盖所有其他位置



我正在尝试编写一个简单的nginx配置。我需要的是:

  1. 如果文件存在于根目录中,则提供此文件
  2. 如果 url 是/default/url,则显示/some/path2/index.html
  3. 否则重定向到/default/url

我的配置如下

server {
listen 127.0.0.1:80;
server_name my.domain.com;
root /some/path/html;
location / {
return 302 /default/url; 
}
location = /default/url {
rewrite ^/(.*)$/some/path2/index.html;
}
location /default/e_schema {
proxy_pass http://other.host.com;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

无论网址如何,它都会立即重定向到/default/url。 我试图将位置/块放在底部和顶部。我尝试使用位置 ~/.*来降低优先级,但没有任何帮助。如果我删除位置/一切都很好,我的要求 2 和 3 都可以。

根据这个答案 https://serverfault.com/questions/656628/nginx-catch-all-other-locations-than-given/656634 它应该有效。

您在位置块中输入了"=">

location = /default/url {

你能尝试删除它吗?我相信它可能正在设置网址

问题就在这里

location = /default/url {
rewrite ^/(.*)$/some/path2/index.html;
}

这使得内部重定向到/路径内的/some/path2/index.html,因此它会触发重定向到/默认/URL等的位置/块。

我的解决方案是制作空块以从位置/中排除路径

location /some/path2/index.html {}

相关内容

最新更新