Nginx rewrite规则实现http跳转到https及301永久重定向

Nginx rewrite规则实现http跳转到https及301永久重定向

环境准备:

[root@ubuntu1804 ~]#curl https://linux2022.com -Ik
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 07:26:10 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Sat, 20 Aug 2022 04:15:33 GMT
Connection: keep-alive
ETag: "63005fe5-f"
Accept-Ranges: bytes

[root@ubuntu1804 ~]#curl https://www.linux2022.com -Ik
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 07:26:19 GMT
Content-Type: text/html
Content-Length: 11
Last-Modified: Sat, 20 Aug 2022 03:36:17 GMT
Connection: keep-alive
ETag: "630056b1-b"
Accept-Ranges: bytes

1. 实现http跳转到https及301永久重定向

#注意:配置rewrite才能实现http跳转到https
#参数说明:
#一年内实现浏览器自动跳转
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

[root@centos7 ~]#cd /apps/nginx/conf/conf.d/
[root@centos7 conf.d]#ls
mobile.conf  pc.conf  ssl
[root@centos7 conf.d]#vim pc.conf
server{
   listen 80;
   listen 443 ssl;
   ssl_certificate /apps/nginx/conf/conf.d/ssl/www.linux2022.com.crt;
   ssl_certificate_key /apps/nginx/conf/conf.d/ssl/www.linux2022.com.key;
   ssl_session_cache shared:sslcache:20m;
   ssl_session_timeout 10m;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

   server_name www.linux2022.com;
   location / {
     root /data/nginx/html/pc;
     if ( $scheme = http ) {
        rewrite ^/(.*)$ https://www.linux2022.com/$1 permanent;
     }
   }
}

[root@centos7 conf.d]#vim mobile.conf
server{
   listen 80;
   listen 443 ssl;
   ssl_certificate /apps/nginx/conf/conf.d/ssl/linux2022.com.pem;
   ssl_certificate_key /apps/nginx/conf/conf.d/ssl/linux2022.com.key;
   ssl_session_cache shared:sslcache:20m;
   ssl_session_timeout 10m;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

   server_name linux2022.com;
   location / {
     root /data/nginx/html/mobile;
     if ( $scheme = http ) {
        rewrite ^/(.*)$ https://linux2022.com/$1 permanent;
     }
  }
}

[root@centos7 conf.d]#nginx -t
[root@centos7 conf.d]#nginx -s reload

#测试验证
[root@ubuntu1804 ~]#vim /etc/hosts
10.0.0.27 www.linux2022.com linux2022.com
[root@ubuntu1804 ~]#curl -I http://www.linux2022.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 09:09:39 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.linux2022.com/
Strict-Transport-Security: max-age=31536000; includeSubDomains

[root@ubuntu1804 ~]#curl -I http://linux2022.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Sat, 20 Aug 2022 09:22:13 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://linux2022.com/
Strict-Transport-Security: max-age=31536000; includeSubDomains

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直在努力学习的菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值