如何在 nginx 配置中排除特定的子域 server_name [英] How to exclude specific subdomains server_name in nginx configuration
问题描述
我在 server_name
中使用通配符.我想将 example.com
(配置为 *.example.com)的所有子域重定向到 foo.com
除了 xyz.example.com
I'm using wildcard in server_name
. I want to redirect all subdomains of example.com
(configured as *.example.com) to foo.com
except xyz.example.com
我有如下配置
server {
listen 80;
server_name *.example.com;
location / {
proxy_pass http://$1.foo.com;
}
}
我不想更改任何发送到 xyz.example.com
I don't want to change any request coming to xyz.example.com
推荐答案
你至少需要两个服务器块,nginx
会选择更具体的服务器块来处理请求.有关详细信息,请参阅本文档.
You need at least two server blocks, and nginx
will select the more specific server block to handle the request. See this document for details.
您将需要 xyz.example.com
的服务器块,例如:
You will need a server block for xyz.example.com
such as:
server {
listen 80;
server_name xyz.example.com;
location / {
proxy_pass http://$1.foo.com;
}
}
然后是 default_server
或通配符服务器,例如:
Then either a default_server
or a wild card server, such as:
server {
listen 80;
server_name *.example.com;
return http://foo.com/;
}
或者:
server {
listen 80 default_server;
return http://foo.com/;
}
这篇关于如何在 nginx 配置中排除特定的子域 server_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!