2014-10-01 160 views

回答

1

使用wwww設置Nginx中的所有域服務器塊。也就是說,只設置了http://www.example.com等類型的服務器塊,然後創建一個單獨的捕獲所有服務器塊重定向所有http://example.com等類型的請求到http://www.example.com

這是基於配置outlined here

http { 
    [...] 
    # Catch All for http://example.com domains 
    # These will all redirect to http://www.example.com 
    server { 
     listen 80; 
     return 301 http://www.$host$request_uri; 
    } 
    # Other server blocks (http://www.example.com etc) 
    include /etc/nginx/conf.d/*.conf; 
} 

的這種方式的工作原理是,http://example.com的所有請求將始終只能通過捕獲所有塊來提供,並將其彈回到http://www.example.com

注意事項有:

  1. 確保你不會在任何地方使用default_server指令。

  2. 對解析到您的服務器但沒有明確定義的域的請求將向用戶返回重定向循環錯誤。爲避免這種情況,如果出現問題,請確保已解析到服務器的每個域都已定義。