How to exclude specific server_name subdomains in nginx configuration

I am using a wildcard in server_name . I want to redirect all example.com subdomains (configured as * .example.com) to foo.com except xyz.example.com

I have a configuration as follows

 server { listen 80; server_name *.example.com; location / { proxy_pass http://$1.foo.com; } } 

I do not want to change any request to xyz.example.com

+6
source share
1 answer

You need at least two server blocks, and nginx will select a more specific server block to process the request. See this document for more details.

You will need a server block for xyz.example.com , for example:

 server { listen 80; server_name xyz.example.com; location / { proxy_pass http://$1.foo.com; } } 

Then either a default_server or a wild card server, for example:

 server { listen 80; server_name *.example.com; return http://foo.com/; } 

Or:

 server { listen 80 default_server; return http://foo.com/; } 
+4
source

All Articles