• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    【C语言基础】结构体中内嵌联合体|联合体中内嵌结构体
    地震数据处理研究(Matlab代码实现)
    Python中stack和unstack函数(附加reset_index,set_index函数)
    扫雷?拿来吧你(递归展开+坐标标记)
    JVM之类加载器
    特斯拉起诉前工程师:简历造假、窃取超算Dojo机密信息、拿“假”电脑应付检查
    数据库 高阶语句2
    C语言求 MD5 值
    浏览器本地存储之Cookie和webStorage
    软考高级架构师下篇-18大数据架构理论设计与实践
  • 原文地址:https://blog.csdn.net/weixin_51867896/article/details/126442678