【发布时间】:2025-12-24 00:45:10
【问题描述】:
我正在尝试编写一个简单的 nginx 配置。我需要的是:
- 如果根目录中存在文件,则提供此文件
- 如果 url 是 /default/url 则显示 /some/path2/index.html
- 否则重定向到/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。 我试图将 location / 块放在底部和顶部。我尝试使用 location ~ /.* 来降低优先级,但没有任何帮助。如果我完全删除 location / 一切都很好,我的要求 2 和 3 都可以。
根据这个答案https://serverfault.com/questions/656628/nginx-catch-all-other-locations-than-given/656634 它应该可以工作。
【问题讨论】:
标签: nginx nginx-location