如何使用NGINX重定向除主页以外的所有页面?

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

这是我的NGINX conf文件:

server {
    listen 80;
    listen [::]:80;
    server_name other.com;
    root        /home/user/html;

        location = / {

        }

        location / {
           return 301 https://mydom.com$request_uri;
        }
}

它应该重定向除主路由(“ /”)之外的每个请求。但是现在它也将所有内容都重定向到主要路线。我的错误在哪里?

node.js nginx
1个回答
0
投票

您的location = /块隔离了一个URI-原始请求。

默认情况下,Nginx通过检查是否解析为目录,并检查目录中是否有与/指令中列出的文件匹配的文件来处理以index结尾的任何请求(默认值:index.html)。

index指令导致内部重定向,这导致Nginx重复搜索匹配的location

您还需要隔离重定向的请求。

例如:

location = / { }
location = /index.html { }
location / { ... }

或者,绕过index指令,并使用location语句将其处理为单个try_files

例如:

location = / { try_files /index.html =404; }
location / { ... }

有关详细信息,请参见this document

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