Mik*_*keN 542 nginx https redirect
我想将我的 Web 服务器上的所有 http 请求重写为 https 请求,我从以下内容开始:
服务器 {
听80;
地点 / {
重写 ^(.*) https://mysite.com$1 永久;
}
...
一个问题是这会剥离任何子域信息(例如,node1.mysite.com/folder),我如何重写上述内容以将所有内容重新路由到 https 并维护子域?
Sai*_*han 781
原来我对这个问题的第一个答案在某些时候是正确的,但它变成了另一个陷阱 - 要保持最新状态,请查看Taxing rewrite pitfalls
我已经被许多 SE 用户纠正过,所以功劳归于他们,但更重要的是,这里是正确的代码:
server {
listen 80;
server_name my.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
# add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000" always;
[....]
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ale 283
注意:执行此操作的最佳方法是由https://serverfault.com/a/401632/3641提供的- 但在此处重复:
server {
listen 80;
return 301 https://$host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
在最简单的情况下,您的主机将被固定为您要将它们发送到的服务 - 这将执行 301 重定向到浏览器,浏览器 URL 将相应更新。
以下是之前的答案,由于正则表达式而效率低下,如@kmindi 所示,简单的 301 很棒
我一直在使用 nginx 0.8.39 及更高版本,并使用了以下内容:
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
Run Code Online (Sandbox Code Playgroud)
向客户端发送永久重定向。
kmi*_*ndi 129
我认为最好也是唯一的方法应该是使用HTTP 301 Moved Permanently重定向,如下所示:
server {
listen [::]:80;
return 301 https://$host$request_uri;
}
Run Code Online (Sandbox Code Playgroud)
该已移动永久HTTP 301重定向也是最有效的,因为没有正则表达式来进行评估,根据已经提到pitfails。
新的HTTP 308 Moved Permanently保留了 Request 方法,并得到主流浏览器的支持。例如, using308可以防止浏览器将重定向请求的请求方法从POST更改GET为 。
如果您想保留主机名和子域,这就是方法。
如果您没有 DNS,这仍然有效,因为我也在本地使用它。例如,我正在请求http://192.168.0.100/index.php并且将被重定向到https://192.168.0.100/index.php.
我listen [::]:80在我的主机上使用,因为我已经bindv6only设置为false,所以它也绑定到 ipv4 套接字。listen 80如果您不想要 IPv6 或想要绑定到其他地方,请将其更改为。
Saif Bechan 的解决方案server_name在我的情况下使用的是本地主机,但无法通过网络访问。
Michael Neale 的解决方案很好,但根据 pitfails,重定向 301 有更好的解决方案;)
小智 23
在服务器块中,您还可以执行以下操作:
# Force HTTPS connection. This rules is domain agnostic
if ($scheme != "https") {
rewrite ^ https://$host$uri permanent;
}
Run Code Online (Sandbox Code Playgroud)
小智 19
以上不适用于一直创建新的子域。例如 AAA.example.com BBB.example.com 大约 30 个子域。
最终得到了一个使用以下内容的配置:
server {
listen 80;
server_name _;
rewrite ^ https://$host$request_uri? permanent;
}
server {
listen 443;
server_name example.com;
ssl on;
ssl_certificate /etc/ssl/certs/myssl.crt;
ssl_certificate_key /etc/ssl/private/myssl.key;
ssl_prefer_server_ciphers on;
# ...
# rest of config here
# ...
}
Run Code Online (Sandbox Code Playgroud)
很久很久以前,我对正确答案发表了评论,并进行了非常重要的更正,但我觉得有必要在自己的答案中强调这一更正。如果您在任何时候设置了不安全的 HTTP 并期望用户内容、拥有表单、托管 API,或者已配置任何网站、工具、应用程序或实用程序来与您的站点对话,那么之前的所有答案都不能安全使用。
当POST向您的服务器发出请求时会出现问题。如果服务器响应带有简单的30x重定向,则 POST 内容将丢失。发生的情况是浏览器/客户端将请求升级为 SSL,但将其降级POST为GET请求。该POST参数将丢失和不正确的请求将您的服务器进行。
解决方法很简单。您需要使用HTTP 1.1 307重定向。这在 RFC 7231 S6.4.7 中有详细说明:
Note: This status code is similar to 302 (Found), except that it
does not allow changing the request method from POST to GET. This
specification defines no equivalent counterpart for 301 (Moved
Permanently) ([RFC7238], however, defines the status code 308
(Permanent Redirect) for this purpose).
Run Code Online (Sandbox Code Playgroud)
根据已接受的解决方案改编的解决方案是307在您的重定向代码中使用:
server {
listen 80;
server_name my.domain.com;
return 307 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name my.domain.com;
# add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000";
[....]
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我在 AWS ELB 后面运行 ngnix。ELB 正在通过 http 与 ngnix 通信。由于 ELB 无法向客户端发送重定向,因此我检查 X-Forwarded-Proto 标头并重定向:
if ($http_x_forwarded_proto != 'https') {
return 301 "https://www.exampl.com";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
530981 次 |
| 最近记录: |