2017-07-27 61 views
-1

我找不到一個特定的重寫規則im嘗試的例子。NGINX通配符重寫在同一個服務器模塊

我想有一個重寫規則,例如只輸入子域名。

https://sub.example.com/ - >https://example.com/directory1/directory2/sub

從香港專業教育學院一直在努力一下這個看起來像最近的代碼我有。

server_name example.com; 

if ($host = *.example.com) 
    return 301 https://example.com/directory1/directory2/$1; 
} 
+0

[nginx子域重寫]的可能重複(https://stackoverflow.com/questions/2498712/nginx-subdomain-rewrite) –

回答

0

在文檔看看:


這將是一個解決辦法:

server_name .example.com; 
if ($host ~ ^(?<sub>.+)\.example\.com$) { 
    return 301 http://example.com/directory1/directory2/$sub; 
} 

這將是另一種解決方案:

server_name ~^(?<sub>.+)\.example\.com$; 
return 301 http://example.com/directory1/directory2/$sub; 

哪種方案更好?很難說 - 它取決於您收到的流量模式,以及在同一個監聽句柄上配置了哪些其他服務器。

+0

上述工作完美,我試圖改變它重寫,而不是回報,讓我不要讓ssl證書對子域名無效。有沒有辦法繞過這個問題? – breebee2

+0

我不認爲你應該在這裏使用重寫(如果你需要請求的URI,使用'$ request_uri');但對於SSL來說沒有任何差別;你顯然必須爲所有子域擁有適當的證書才能在沒有警告的情況下工作(或者不要使用SSL!)。 – cnst