Linux下nginx服务实现http到https的自动重定向
原创Cinjosy 最后发布于2019-04-26 16:57:14 阅读数 300 收藏
展开
续我的上篇博文:https://mp.csdn.net/postedit/89552895。即本篇博文是在上篇博文修改完之后的nginx.conf文件中进行修改的。
实现http到https的自动重定向
1、实现临时重定向
(1)编写nginx.conf文件
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf #其中116-135是上上篇博文配置的虚拟主机,137及以后的行是上篇博文配置的https。将145,147,148行和149行注释,添加140行的内容
116 # HTTPS server
117 #
118 server {
119 listen 443 ssl;
120 server_name xin.westos.org;
121
122 ssl_certificate cert.pem;
123 ssl_certificate_key cert.pem;
124
125 ssl_session_cache shared:SSL:1m;
126 ssl_session_timeout 5m;
127
128 ssl_ciphers HIGH:!aNULL:!MD5;
129 ssl_prefer_server_ciphers on;
130
131 location / {
132 root /web;
133 index index.html index.htm;
134 }
135 }
136
137 server {
138 listen 80;
139 server_name xin.westos.org;
140 rewrite ^/(.*)$ https://xin.westos.org;
141 #set_real_ip_from 172.25.83.0/24;
142 #real_ip_header X-Forwarded-For;
143 #real_ip_recursive on;
144
145 #location / {
146 #return 200 "client real ip: $remote_addr\n";
147 #root /web;
148 #index index.html;
149 #}
150 }
151 }
[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload #修改完配置文件之后,重载nginx服务
(2)进行测试:
测试一:在命令行进行测试
[root@server1 ~]# curl -I xin.westos.org #进行测试
HTTP/1.1 302 Moved Temporarily #我们可以看到302临时重定向
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 07:57:18 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://xin.westos.org #我们可以看到实现了自动跳转(xin.westos.org——>https://xin.westos.org)
测试二:在浏览器中访问:http://xin.westos.org/,帮我们自动跳转到了下面的界面。
2、对实现临时重定向的内容进行改进
在进行改进之前,我们进行一个测试,来看出改进的必要性
[root@server1 ~]# cd /web/
[root@server1 web]# ls
index.html
[root@server1 web]# echo test > test.html
[root@server1 web]# curl -I xin.westos.org/test.html
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 08:25:49 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://xin.westos.org #在这里,我们会发现一个问题(我们访问的是xin.westos.org/test.html却帮我们自动跳转到了https://xin.westos.org),这显然是不合理的
在浏览器中访问:http://xin.westos.org/test.html,却帮我们自动跳转到了下面的界面。
下面,我们进行改进
(1)编写nginx.conf文件
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf #在140行的后面加入"/$1"
116 # HTTPS server
117 #
118 server {
119 listen 443 ssl;
120 server_name xin.westos.org;
121
122 ssl_certificate cert.pem;
123 ssl_certificate_key cert.pem;
124
125 ssl_session_cache shared:SSL:1m;
126 ssl_session_timeout 5m;
127
128 ssl_ciphers HIGH:!aNULL:!MD5;
129 ssl_prefer_server_ciphers on;
130
131 location / {
132 root /web;
133 index index.html index.htm;
134 }
135 }
136
137 server {
138 listen 80;
139 server_name xin.westos.org;
140 rewrite ^/(.*)$ https://xin.westos.org/$1;
141 #set_real_ip_from 172.25.83.0/24;
142 #real_ip_header X-Forwarded-For;
143 #real_ip_recursive on;
144
145 #location / {
146 #return 200 "client real ip: $remote_addr\n";
147 #root /web;
148 #index index.html;
149 #}
150 }
151 }
[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload #修改完配置文件之后,重载nginx服务
(2)进行测试:
测试一:在命令行进行测试
[root@server1 ~]# curl -I xin.westos.org/test.html
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 08:30:21 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://xin.westos.org/test.html #我们会发现,实现了xin.westos.org/test.html——>https://xin.westos.org/test.html的自动跳转
测试二:在浏览器中访问:http://xin.westos.org/test.html,帮我们自动跳转到了下面的界面。
3、实现永久重定向
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf #在140行去掉"/$1"并在最后添加permanent(永久)
116 # HTTPS server
117 #
118 server {
119 listen 443 ssl;
120 server_name xin.westos.org;
121
122 ssl_certificate cert.pem;
123 ssl_certificate_key cert.pem;
124
125 ssl_session_cache shared:SSL:1m;
126 ssl_session_timeout 5m;
127
128 ssl_ciphers HIGH:!aNULL:!MD5;
129 ssl_prefer_server_ciphers on;
130
131 location / {
132 root /web;
133 index index.html index.htm;
134 }
135 }
136
137 server {
138 listen 80;
139 server_name xin.westos.org;
140 rewrite ^/(.*)$ https://xin.westos.org permanent;
141 #set_real_ip_from 172.25.83.0/24;
142 #real_ip_header X-Forwarded-For;
143 #real_ip_recursive on;
144
145 #location / {
146 #return 200 "client real ip: $remote_addr\n";
147 #root /web;
148 #index index.html;
149 #}
150 }
151 }
[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload #修改完配置文件之后,重载nginx服务
测试:
测试一:在命令行进行测试
[root@server1 ~]# curl -I xin.westos.org #进行测试
[root@server1 ~]# curl -I xin.westos.org
HTTP/1.1 301 Moved Permanently #我们可以看到301永久重定向
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 08:01:26 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://xin.westos.org #我们可以看到实现了自动跳转(xin.westos.org——>https://xin.westos.org)
测试二:在web浏览器进行测试,输入xin.westos.org,看能否自动跳转到https://xin.westos.org。
4、对实现永久重定向的内容进行改进
在进行改进之前,我们进行一个测试,来看出改进的必要性
[root@server1 ~]# curl -I xin.westos.org/test.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 08:35:04 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://xin.westos.org/ #在这里,我们会发现一个问题(我们访问的是xin.westos.org/test.html却帮我们自动跳转到了https://xin.westos.org),这显然是不合理的
在浏览器中访问:http://xin.westos.org/test.html,却帮我们自动跳转到了下面的界面。
下面,我们进行改进
(1)编写nginx.conf文件
[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf #在140行的xin.westos.org后面加入"/$1"
116 # HTTPS server
117 #
118 server {
119 listen 443 ssl;
120 server_name xin.westos.org;
121
122 ssl_certificate cert.pem;
123 ssl_certificate_key cert.pem;
124
125 ssl_session_cache shared:SSL:1m;
126 ssl_session_timeout 5m;
127
128 ssl_ciphers HIGH:!aNULL:!MD5;
129 ssl_prefer_server_ciphers on;
130
131 location / {
132 root /web;
133 index index.html index.htm;
134 }
135 }
136
137 server {
138 listen 80;
139 server_name xin.westos.org;
140 rewrite ^/(.*)$ https://xin.westos.org/$1 permanent;
141 #set_real_ip_from 172.25.83.0/24;
142 #real_ip_header X-Forwarded-For;
143 #real_ip_recursive on;
144
145 #location / {
146 #return 200 "client real ip: $remote_addr\n";
147 #root /web;
148 #index index.html;
149 #}
150 }
151 }
[root@server1 ~]# /usr/local/nginx/sbin/nginx -s reload #修改完配置文件之后,重载nginx服务
(2)进行测试:
测试一:在命令行进行测试
[root@server1 ~]# curl -I xin.westos.org/test.html
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 26 Apr 2019 08:36:43 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://xin.westos.org/test.html #我们会发现,实现了xin.westos.org/test.html——>https://xin.westos.org/test.html的自动跳转
测试二:在浏览器中访问:http://xin.westos.org/test.html,帮我们自动跳转到了下面的界面。
原文链接:https://blog.csdn.net/qq_42303254/article/details/89554311