浏览器忽略来自nginx重定向的新端口
问题描述:
我需要通过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检查了它。结果是一样的,我不明白为什么?
答
你有HSTS(Strict-Transport-Security
标题)。所以,浏览器做的正是这样的政策要求:
https://tools.ietf.org/html/rfc6797#section-8.3
8.3.5 [...]的UA 必须以 “https” 替换URI方案,和[...]如果URI包含不等于“80”的显式端口组件,必须保留端口组件值; ...
您是否启用了HSTS? –
443记录包含'add_header Strict-Transport-Security max-age = 15768000;',HSTS已启用。 – Shutterfly
恩,这就是原因。 https://tools.ietf.org/html/rfc6797#section-8.3 _5。 [...] UA必须用“https”替换URI方案,并且如果URI包含不等于“80”的显式端口组件,则端口组件值必须被保留 –