2015-04-04 92 views
1

我按照https://stackoverflow.com/a/11733363/2532070的指示將www重定向到非www。我試圖重定向以下格式:Nginx www。非www。重定向不起作用

http://example.com 
http://www.example.com 
https://www.example.com 

大家:

https://example.com 

http://example.com被重定向到https。但是,另外兩個,www.不是。這裏是我的nginx.conf:

upstream app_server { 
    server 127.0.0.1:9000 fail_timeout=0; 
} 
# 
# Redirect all www to non-www 
# 
server { 
    server_name   www.example.com; 
    ssl_certificate  /src/bin/ssl/ssl-bundle.crt; 
    ssl_certificate_key /etc/ssl/private/STAR_example_com.key; 
    listen    *:80; 
    listen    *:443 ssl spdy; 
    listen    [::]:80 ipv6only=on; 
    listen    [::]:443 ssl spdy ipv6only=on; 

    return 301 https://example.com$request_uri; 
} 

# 
# Redirect all non-encrypted to encrypted 
# 
server { 
    server_name   example.com; 
    listen    *:80; 
    listen    [::]:80; 

    return 301 https://example.com$request_uri; 
} 

# 
# There we go! 
# 
server { 
    server_name   example.com; 
    ssl_certificate  /src/bin/ssl/ssl-bundle.crt; 
    ssl_certificate_key /etc/ssl/private/STAR_example_com.key; 
    listen    *:443 ssl spdy; 
    listen    [::]:443 ssl spdy; 

    # rest goes here... 

    root /usr/share/nginx/html; 
    index base.html index.html index.htm; 

    client_max_body_size 4G; 

    keepalive_timeout 5; 

    # Your Django project's media files - amend as required 
    location /media { 
     alias /src/media; 
    expires 1y; 
    add_header Cache-Control "public"; 
    } 

    # your Django project's static files - amend as required 
    location /static { 
     alias /src/static; 
    expires 1y; 
    add_header Cache-Control "public"; 
    } 

    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://app_server; 
    } 
} 

我沒有看到在nginx的日誌文件,以指示錯誤什麼。當我嘗試訪問www.版本時是否有某處要查找錯誤?謝謝!

+0

您是否擁有www.example.com的DNS記錄? – 2015-04-05 22:49:02

回答

3

您很可能遇到瀏覽器緩存問題。嘗試清除緩存,例如在Chrome開發工具網絡選項卡或Firefox的開發工具設置中檢查禁用緩存。這應該解決它。

+0

感謝您的幫助!試過了,沒有幫助。我得到一個'無法加載資源:net :: ERR_NAME_NOT_RESOLVED'錯誤仍然(也許這個瀏覽器錯誤有幫助?)。 – YPCrumble 2015-04-04 21:25:09

+0

這表示DNS問題,請仔細檢查您的nginx配置中的域名中是否有任何錯別字。 – Fleshgrinder 2015-04-04 21:51:28

+0

謝謝 - 這是否意味着我需要將www.example.com指向我的IP地址,例如我有example.com? (也許這是一個初學者的錯誤?) – YPCrumble 2015-04-04 21:54:01