2016-11-18 64 views
1

我有一個子,我嘗試使用nginx的,一些子域名被服務使用SSL,有些則不是服務於多個網站。我在使非SSL網站正常運行方面遇到了一些麻煩。每當我嘗試訪問它們時,它們都會立即將(使用正確的主機)重定向到SSL/HTTPS版本。我在下面附加了我的位置塊。我已經閱讀了nginx塊的請求處理,但無法弄清楚如何強制未加密的主機不能轉發。 (http://nginx.org/en/docs/http/request_processing.html的Nginx服務器塊排序

server { 
    listen 80; 
    server_name dev.example.ca dev.example.server2.example.tl; 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location/{ 
     include proxy_params; 
     proxy_pass http://unix:/home/example/example/socket.sock; 
    } 
    location /static { 
     autoindex on; 
     alias /home/litobro/example/example/static/; 
    } 
} 

server { 
    listen 80; 
    server_name dutyroster.example.ca; 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location/{ 
     include proxy_params; 
     proxy_pass http://unix:/home/example2/dutyroster/socket.sock; 
    } 
    location /static { 
     autoindex on; 
     alias /home/example/example2/static/; 
    } 

    location /socket.io { 
     proxy_pass http://unix:/home/example/example2/socket.sock; 
     proxy_redirect off; 
     proxy_buffering off; 

     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection "Upgrade"; 
    } 
} 

server { 
    listen 80; 
    server_name ex3.server2.example.tl example.ca www.example.ca; 

    location ~ .well-known/acme-challenge/ { 
     root /var/www/letsencrypt; 
     default_type text/plain; 
    } 

    return 301 https://$host$request_uri; 
} 

server { 
    listen 443 ssl http2; 
    server_name example.ca www.example.ca example.server2.example.tl; 

    ssl_certificate /etc/letsencrypt/live/example.ca/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/example.ca/privkey.pem; 
    include snippets/ssl-params.conf; 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location/{ 
     include proxy_params; 
     proxy_pass http://unix:/home/example/example3/socket.sock; 
    } 
} 

我訂購的服務器模塊,使得80端口的請求將有希望獲得先出手,但這似乎仍無法正常工作。先謝謝您的幫助! (服務器塊大多剝離實際域,但我想我已經找到了替代域一致性)

+0

這做到了!我在ssl-params中包含了子域名。謝謝一堆!你能否提出這個答案,以便我可以標記這個解決方案? – Litobro

回答

0

該問題的原因是HSTS頭,它被設置內部snippets/ssl-params.conf。該標題告訴瀏覽器該網站將僅通過HTTPS進行連接。以下是這頭是如何設置與Nginx的一個例子:

add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;"; 

如果頭值包含includeSubDomains標誌,就像上面的例子,那麼HSTS政策也將適用於主域的所有子域。這就是您的瀏覽器試圖通過HTTPS將所有請求發送到子域的原因。

請記住,現代瀏覽器HSTS網站的列表存儲在一個特殊的緩存,所以簡單地刪除或修改Nginx的頭部不會造成任何立竿見影的效果。您將需要以特定於瀏覽器的方式手動清除HSTS緩存。

還值得一提的是,使用includeSubDomains標誌被認爲是good practice,因此保留它併爲您的子域頒發證書可能是一個好主意。目前有幾個證書頒發機構,如Let's Encrypt,它們提供免費且易於安裝的證書。

0

對不起,我沒有與nginx的許多經驗,但你在年底有兩個服務器阻止你conf文件。第一次偵聽端口80,你有「返回301 https:// $ host $ request_uri;」這條線是否需要?

嘗試註釋該行,看看你的非SSL服務器名稱仍然重定向到HTTPS。

+0

我使用它來強制某些主機強制使用ssl,當我將該行刪除時,這些主機不再被強制,但其他主機也可以在沒有ssl的情況下工作。由於某種原因,它優先於其他主機,我不知道爲什麼。 – Litobro