如何重定向Nginx中除一条路径之外的所有请求?

问题描述 投票:0回答:1

我想在服务器上保留一条路径,但我想将所有其他请求重定向到另一个域。如何成功服务给定位置的所有请求,但所有其他请求都重定向到 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
}
nginx nginx-config
1个回答
0
投票

这种模式似乎有效,尽管我可以通过正则表达式获得更多创意。

location / {
   rewrite ^/(.*)$ https://example.com$request_uri permanent;
}

location /path/to/keep/ {
   try_files $uri $uri/ /index.html
}

我认为它之所以有效是因为 Nginx 处理优先级的方式。

© www.soinside.com 2019 - 2024. All rights reserved.