How to exclude specific subdomains server_name in nginx configuration

前端 未结 2 525
天命终不由人
天命终不由人 2020-12-15 09:45

I\'m using wildcard in server_name. I want to redirect all subdomains of example.com (configured as *.example.com) to foo.com except <

相关标签:
2条回答
  • 2020-12-15 10:15

    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.

    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;
        }
    }
    

    Then either a default_server or a wild card server, such as:

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

    Or:

    server {
        listen 80 default_server;
        return http://foo.com/;
    }
    
    0 讨论(0)
  • 2020-12-15 10:32

    The '*' wildcard character gets ignored by nginx:

    nginx: [warn] conflicting server name "*.example.com" on 0.0.0.0.:80, ignored

    0 讨论(0)
提交回复
热议问题