使用通配符子域时,Nginx在单个子域上重定向单页

时间:2017-04-08 16:50:02

标签: redirect nginx subdomain wildcard-subdomain

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

服务器块看起来像这样:

header h1 {
  font-size: 1.5em;
}

我需要这种行为:

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

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

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

1 个答案:

答案 0 :(得分: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

相关问题