2016-06-08 48 views
0

我需要通過nginx創建http重定向。我們的目標是:瀏覽器忽略來自nginx重定向的新端口

http://subdomain.domain.tld/path - >https://subdomain.domain.tld/path http://subdomain.domain.tld:8090/path - >https://subdomain.domain.tld/path

這是我的nginx的配置:

server { 
    listen 80; 
    listen 8090; 
    server_name subdomain.domain.tld; 
    return 301 https://$host$request_uri; 
} 

server { 
    listen 443; 
    server_name subdomain.domain.tld; 

    [...] 
} 

第一種情況正常工作。第二種情況在技術上,但不在客戶端瀏覽器上。如果我開始的請求通過捲曲一切都看起來不錯:

curl -v "http://subdomain.domain.tld:8090/path" 
* About to connect() to subdomain.domain.tld port 8090 (#0) 
* Trying XX.XX.XX.XX... 
* Adding handle: conn: 0x1c94010 
* Adding handle: send: 0 
* Adding handle: recv: 0 
* Curl_addHandleToPipeline: length: 1 
* - Conn 0 (0x1c94010) send_pipe: 1, recv_pipe: 0 
* Connected to subdomain.domain.tld (89.15.246.188) port 8090 (#0) 
> GET /oath HTTP/1.1 
> User-Agent: curl/7.30.0 
> Host: subdomain.domain.tld:8090 
> Accept: */* 
> 
< HTTP/1.1 301 Moved Permanently 
* Server nginx is not blacklisted 
< Server: nginx 
< Date: Wed, 08 Jun 2016 10:28:15 GMT 
< Content-Type: text/html 
< Content-Length: 178 
< Connection: keep-alive 
< Location: https://subdomain.domain.tld/path 
< 
<html> 
<head><title>301 Moved Permanently</title></head> 
<body bgcolor="white"> 
<center><h1>301 Moved Permanently</h1></center> 
<hr><center>nginx</center> 
</body> 
</html> 

但是,如果我在瀏覽器中打開URL http://subdomain.domain.tld:8090/path我得到的URL https://subdomain.domain.tld:8090/path。瀏覽器切換到https,但使用舊的端口。

我用Google Chrome,Firefox,Internet Explorer和Apple Safari檢查了它。結果是一樣的,我不明白爲什麼?

+0

您是否啓用了HSTS? –

+0

443記錄包含'add_header Strict-Transport-Security max-age = 15768000;',HSTS已啓用。 – Shutterfly

+0

恩,這就是原因。 https://tools.ietf.org/html/rfc6797#section-8.3 _5。 [...] UA必須用「https」替換URI方案,並且如果URI包含不等於「80」的顯式端口組件,則端口組件值必須被保留 –

回答

2

你有HSTS(Strict-Transport-Security標題)。所以,瀏覽器做的正是這樣的政策要求:

https://tools.ietf.org/html/rfc6797#section-8.3

8.3.5 [...]的UA 必須以 「https」 替換URI方案,和[...]如果URI包含不等於「80」的顯式端口組件,必須保留端口組件值; ...