2015-12-21 95 views
0

如何配置nginx重寫<somesubdomain>.mydomain.commydomain.com/some/url/path/<somesubdomain>/將通配符子域重寫爲具體的頂級域

somesubdomain是通配符子域。

主要要求是NOT REDIRECT,<somesubdomain>.mydomain.com應該是mydomain.com/some/url/path/<somesubdomain>/的掩碼。

此外,訪問其他不同於/(如somesubdomain.mydomain.com/test/)的網址不應顯示任何內容。

請注意,我已經配置爲mydomain.com/代理,所以/some/url/path/<somesubdomain>/應通過並經其代理服務器解析:

location/{ 
     proxy_pass   http://app_servers; 
     proxy_redirect  off; 
     proxy_set_header Host $http_host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Forwarded-Host $server_name; 
     proxy_connect_timeout 10; 
     proxy_read_timeout 10; 
    } 

回答

0

它的工作原理與此配置(由當前server{}部分加):

if ($host ~* (?<subdomain>[a-z0-9]+)\.mydomain\.com) { 
    rewrite ^/$ /some/path/$store_subdomain break; 
} 
1
server { 
    # server name with regexp 
    server_name ~^(?<sub>[^.]+)\.mydomain\.com$; 
    # now this server will catch all requests to xxxx.mydomain.com 
    # and put "xxxx" to $sub variable 

    # location _only_ for "/" URI 
    # we can do it using "=" sign (means "exactly") 
    location =/{ 
     # finally we want to request different URI from remote server 
     proxy_pass http://app_servers/some/url/path/$sub/; 
     # proxy_redirect will rewrite Location: header from backend 
     # or you can leave proxy_redirect off; 
     proxy_redirect http://app_servers/some/url/path/$sub/ http://$sub.mydomain.com/; 
     .... 
    } 
    # next location for all other requests 
    location/{ 
     return 404; 
    } 
}