Nginx使用重写301规则将HTTP重定向到HTTPS
时间:2020-01-09 10:42:18 来源:igfitidea点击:
问题:
我已将nginx设置为安全的反向代理服务器。
如何在nginx Web服务器下将所有http://example.com/请求(流量)重定向到https://example.com/?
如何配置Nginx将HTTP重定向到HTTPS?
答:
您可以使用Nginx Web服务器轻松地将所有http请求重写/重定向到https。
语法如下。
您需要在位置或者服务器指令中添加以下内容。
本教程介绍了如何在Nginx中将HTTP通信重定向到HTTPS。
Nginx重定向HTTP到HTTPS
现在,您已经为Nginx配置并安装了SSL证书,是时候丢弃所有HTTP通信并将用户发送到HTTPS版本了。
编辑nginx.conf文件:
sudo vi /etc/nginx/nginx.conf
if ($host ~* ^(example\.com|www\.example\.com)$ ){
rewrite ^/(.*)$ https://example.com/ permanent;
}
或者最好使用以下重写:
rewrite ^ https://$server_name$request_uri? permanent;
或者使用新语法(推荐):
return 301 https://$server_name$request_uri;
使用Nginx服务器将所有HTTP请求重定向到HTTPS
编辑您的nginx.conf文件,执行:
# vi nginx.conf
您需要按以下方式定义http和https服务器:
################################
## our HTTP server at port 80 ##
################################
server {
listen 80 default;
## set up domain name here ##
server_name www.theitroad.local theitroad.local
access_log off;
error_log off;
##** nginx redirect ALL http requests to https ** ##
return 301 https://$server_name$request_uri;
}
#########################################################################
## Our HTTPS server at port 443. You need to provide ssl config below ###
#########################################################################
server {
access_log logs/theitroad.local/ssl_access.log main;
error_log logs/theitroad.local/ssl_error.log;
index index.html;
root /usr/local/nginx/html;
## start ssl config ##
listen 443 http2 ssl;
server_name www.theitroad.local theitroad.local
## redirect www to nowww
if ($host = 'www.theitroad.local' ) {
rewrite ^/(.*)$ https://theitroad.local/ permanent;
}
### ssl config - customize as per your setup ###
ssl_certificate ssl/theitroad.local/theitroad.local_combined.crt;
ssl_certificate_key ssl/theitroad.local/theitroad.local.key_without_password;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
keepalive_timeout 70;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
### Rest of my config. It is optional. Do it only if you have Apache on backend ##
## PROXY backend
location / {
add_header Front-End-Https on;
add_header Cache-Control "public, must-revalidate";
add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
proxy_pass http://theitroadproxy;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存并关闭文件。
重新加载或者重启nginx服务器:
# nginx -s reload
测试一下:
$ curl -I http://theitroad.local $ curl -I http://theitroad.local/foo/bar/file.html
输出示例:
在Nginx服务器中将301 HTTP重写为HTTPS
重定向所有HTTP流量
在您的nginx.conf中编辑或者添加如下内容:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
return指令停止处理并将指定的代码返回给客户端。
非标准代码444关闭连接而不发送响应头。
在上面的示例中,我们返回HTTP代码301:
return code URL; return 301 URL; return 301 URL;
可以使用以下代码:
- HTTP/301 HTTP响应状态代码301"已永久移动"用于永久URL重定向
- HTTP/302找到的HTTP响应状态代码302是执行带有"临时移动"代码的URL重定向的常用方法。
另外,可以使用代码302将用于临时重定向的URL指定为唯一参数。
这样的参数应以http://,https://或者$scheme字符串开头。

