2015-07-21 84 views
6

此刻我運行一個網站在 的nginx/1.6.3centOS7Nginx的SSL /子/ IP重新定向

一切運行平穩,除了一些重定向。

這是我的.conf文件是什麼樣子:

server { 
    listen 443 ssl spdy default deferred; 
    server_name .example.com; 

    ... more configs 

    } 


server { 
    listen 80; 
    server_name .example.com; 
    return 301 https://example.com$request_uri; 
} 

我想什麼來實現有以下幾種情況:

user visits in browser    | this should happen 
------------------------------------|------------------------------------- 
https://example.com$request_uri  | Just deliver content 
https://*.example.com$request_uri | 301 https://example.com$request_uri 
https://123.123.123.123$request_uri | 301 https://example.com$request_uri 
http://example.com$request_uri  | 301 https://example.com$request_uri 
http://*.example.com$request_uri | 301 https://example.com$request_uri 
http://123.123.123.123$request_uri | 301 https://example.com$request_uri 

回答

1

,請查看與下面的配置運行,這應該工作。

#This would serve all your content. 
server { 
    listen 443 ssl spdy default deferred; 
    server_name example.com; 

    ... more configs 

} 

#https calls to anything except example.com would be redirected here  
server { 
    listen 443 ssl spdy default deferred; #(Can also use only : "listen 443;") 
    server_name *.example.com 123.123.123.123; 
    return 301 https://example.com$request_uri; 
} 

#All port 80 redirection to https://example.com 
server { 
    listen 80; 
    server_name example.com *.example.com 123.123.123.123; 
    return 301 https://example.com$request_uri; 
} 
0

我用這個模式來解決類似的問題:

server_name ~^(?<subdomains>.+\.)?(?<domain>[^.]+\.[^.]+)$; 
if ($domain != 'example.com') { 
     rewrite ^/(.*)$ http://${subdomains}example.com/$1 permanent; 
} 

等等