【问题标题】:Nginx redirect for single page on single subdomain while using wildcard subdomainsNginx 在使用通配符子域时重定向单个子域上的单个页面
【发布时间】:2017-09-03 23:59:12
【问题描述】:

我有一个使用通配符子域的服务器块,例如 *.example.com。我需要将单个子域上的单个页面重定向到不同的页面,同时正常处理所有其他请求。处理此问题的最有效方法是什么?

服务器块看起来像这样:

server {
    listen 80;
    server_name *.example.com;
    ....
}

我需要这种行为:

http://baseball.example.com/test.php -> http://baseball.example.com/new-page

http://football.example.com/test.php -> 无重定向

http://basketball.example.com/test.php -> 无重定向

【问题讨论】:

    标签: redirect nginx subdomain wildcard-subdomain


    【解决方案1】:

    从处理的角度来看,最有效的方法是为特殊情况创建一个单独的 server 块。由于两个服务器块的大部分配置相同,您可以使用include 语句从单独的文件中读取它。

    例如:

    server {
        listen 80;
        server_name *.example.com;
        include /path/to/common.config;
    }
    server {
        listen 80;
        server_name baseball.example.com;
        location = /test.php {
            return 301 /new-page;
        }
        include /path/to/common.config;
    }
    

    请参阅this document 了解更多信息。

    【讨论】:

    • 好主意,我认为这是最好的选择。感谢您的提示!
    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 2016-04-20
    • 2014-05-29
    • 2020-09-27
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多