2015-04-04 10 views
2

我沒有主域名的證書,所以我對我的幾個開發域使用自簽證書。如何在默認nginx服務器中完全禁用/重寫HTTPS?

我用這樣的:(前https://anyhost.somehost.com/

server { 
    listen  443; 
    server_name secure.somehost.com; 

    ssl on; 
    ssl_certificate /etc/nginx/ssl/secure.crt; 
    ssl_certificate_key /etc/nginx/ssl/secure.key; 
} 
server { 
    listen   80; 
    server_name secure.somehost.com; 
    rewrite  ^https://$server_name$request_uri? permanent; 
} 

現在我所有的HTTPS請求在某種程度上改寫爲https://secure.somehost.com。所以我想要禁用這種行爲並關閉默認服務器的HTTPS,理想情況下將重寫發送到簡單的HTTP(或者只發送500錯誤)。

我試過這段代碼。但由於我沒有證書,我想我無法發送適當的迴應。

server { 
    listen 443 default; 
    rewrite ^(.*) http://$host$1 permanent; 
} 

我必須使用default服務器,因爲我也處理很多子域和外部域。

回答

1

如果你想一個https請求(端口443)重定向到http(端口80上),你應該包括在服務器塊中的SSL證書和密鑰的指令,例如:

# redirect all default http requests to http://example.com 
server { 
    listen 80 default_server; 
    server_name _; 
    access_log off; 
    return 301 $scheme://example.com$request_uri; 
} 

# redirect all https requests to http://example.com 
server { 
    listen 443 ssl; 
    server_name _; 

    # ssl certificate and private key 
    ssl_certificate /etc/nginx/ssl/secure.crt; 
    ssl_certificate_key /etc/nginx/ssl/secure.key; 

    # you can add additional ssl parameters here (like the protocols 
    # and ciphers you want nginx to use). 

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

剛將重定向的目標URL更改爲您要使用的默認域/子域。

如果您使用自簽名證書,瀏覽器仍會提示您並說證書不可信。一旦將其作爲例外添加到瀏覽器的緩存中,連接應該可以正常工作,而瀏覽器不會每次都停止將其標記爲不可信。

+0

事情是我不能使用主域的自簽名證書,因爲我不希望用戶看到瀏覽器警告消息。我也處理很多域名,所以我真的不能爲所有域名生成證書。我只想要https關閉。 – Bobelev 2015-04-04 16:58:59

+0

您可以省略443的服務器塊.Nginx僅監聽您在配置中定義的端口。如果您沒有443的服務器塊,nginx將不會使用該端口。但是,如果你的用戶轉到「https:」//...你的域名,他們將無法獲得任何東西(瀏覽器可能會說他們找不到該域名)。如果您需要將443重定向到80,則至少需要一個ssl證書(來自CA,而不是自簽名),因此您可以在不發出警告的情況下重定向請求。 – kchan 2015-04-04 17:14:50

+0

如果我需要一個服務器塊443的特定子域,我該怎麼辦?也許有任何解決方案? – Bobelev 2015-04-04 18:55:33