我有一个 301 重定向,将所有子域写入 https。现在客户端想要一个仅限 http 的子域。我应该如何在 https 服务器中捕获此请求并将其重定向到 http?
server {
listen 80;
charset utf-8;
server_name *.example.com;
# need to catch specific subdomain here and redirect it to http permanent.
# i know this is wrong, but it illustrates what i must do
if($host == 'subdomain.example.com'){
return 301 http://subdomain.example.com$request_uri?$query_string;
}
# else continue redirect as normal
return 301 https://example.com$request_uri?$query_string;
}
答案1
您应该定义多个server
块。请参阅这一页为server_name
优先。
server {
listen 80;
server_name subdomain.example.com;
# ... what you want to serve at http://subdomain.example.com/ ...
}
server {
listen 80;
server_name example.com *.example.com;
return 301 https://example.com$request_uri?$query_string;
}
server {
listen 443 ssl;
server_name example.com;
# ... what you want to serve at https://example.com/ ...
}
server {
listen 443 ssl;
server_name subdomain.example.com;
return 301 http://subdomain.example.com$request_uri?$query_string;
}