我想在服务器上保留一条路径,但我想将所有其他请求重定向到另一个域。如何成功服务给定位置的所有请求,但所有其他请求都重定向到 example.com?
server {
root /var/www/my_example/html;
index index.html;
location /path/to/keep/ {
try_files $uri $uri/ /index.html
}
# everywhere else redirect requests to domain.com
}
这种模式似乎有效,尽管我可以通过正则表达式获得更多创意。
location / {
rewrite ^/(.*)$ https://example.com$request_uri permanent;
}
location /path/to/keep/ {
try_files $uri $uri/ /index.html
}
我认为它之所以有效是因为 Nginx 处理优先级的方式。