• rewrite


    使用场景

    1. URL访问跳转,支持开发设计(页面跳转、兼容性支持、展示效果等)
    2. SEO优化
    3. 维护(后台维护、流量转发等)
    4. 安全(伪静态)

    正则表达式测试工具

     //安装pcretest
     yum install -y pcre-tools
    
     [root@c2 conf]# pcretest
    PCRE version 8.32 2012-11-30
    
      re> /(d+).(d+).(d+).(d+)/        /正则表达式/
    data> 192.168.88.2
     0: 192.168.88.2
     1: 192
     2: 168
     3: 88
     4: 2

    flag

    • last 停止rewrite检测
    • break 停止rewrite检测
    • redirect 返回302临时重定向,地址栏会显示跳转后的地址
    • permanent 返回301永久重定向,地址栏会显示跳转后的地址

    last与break的区别

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.heboan.com;
            root /opt/app/code;
    
            location ~ ^/break {
                rewrite ^/break /test/ break;
            }
    
            location ~ ^/last {
                rewrite ^/last /test/ last;
            }
    
            location /test/ {
                default_type application/json;
                return 200 '{"status":"success"}';
            }
        }
    }
    nginx.conf

    访问www.heboan.com/test/

    [root@c2 conf]# curl http://www.heboan.com/test/
    {"status":"success"} 

    访问www.heboan.com/break/

    当访问www.heboan.com/break/,匹配到我们定义的location ~ ^/break然后跳转去访问网站根目录/opt/app/code下面的test目录,但是网站根目录下没有test目录,然后返回404

    [root@c2 conf]# curl http://www.heboan.com/break/
    <html>
    <head><title>404 Not Found</title></head>
    <body bgcolor="white">
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx/1.12.2</center>
    </body>
    </html>

    访问www.heboan.com/last/

    当访问www.heboan.com/last/,匹配到我们定义location ~ ^/last然后根据rewrite规则,它会匹配到location /test/,除非它没有匹配到location /test/,才会去网站根目录下去找test目录

    [root@c2 conf]# curl http://www.heboan.com/last/
    {"status":"success"}

    redirect(302)与permanent(301)的区别

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.heboan.com;
            root /opt/app/code;
    
    
            location ~ ^/baidu {
                rewrite ^/baidu01 https://www.baidu.com redirect;
                rewrite ^/baidu02 https://www.baidu.com permanent;
            }
    
    
    
        }
    }
    nginx.conf

    详细查看www.heboan.com/baidu01过程

    [root@c2 conf]# curl -vL www.heboan.com/baidu01
    * About to connect() to www.heboan.com port 80 (#0)
    *   Trying 192.168.88.2...
    * Connected to www.heboan.com (192.168.88.2) port 80 (#0)
    > GET /baidu01 HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: www.heboan.com
    > Accept: */*
    > 
    < HTTP/1.1 302 Moved Temporarily
    < Server: nginx/1.12.2
    < Date: Sun, 15 Apr 2018 14:28:07 GMT
    < Content-Type: text/html
    < Content-Length: 161
    < Connection: keep-alive
    < Location: https://www.baidu.com ######
    < 
    * Ignoring the response-body
    * Connection #0 to host www.heboan.com left intact
    * Issue another request to this URL: 'https://www.baidu.com'
    * About to connect() to www.baidu.com port 443 (#1)
    *   Trying 163.177.151.109...
    * Connected to www.baidu.com (163.177.151.109) port 443 (#1)
    * Initializing NSS with certpath: sql:/etc/pki/nssdb
    *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
      CApath: none
    * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    * Server certificate:
    *     subject: CN=baidu.com,OU=service operation department.,O="BeiJing Baidu Netcom Science Technology Co., Ltd",L=beijing,ST=beijing,C=CN
    *     start date: Jun 29 00:00:00 2017 GMT
    *     expire date: Aug 17 23:59:59 2018 GMT
    *     common name: baidu.com
    *     issuer: CN=Symantec Class 3 Secure Server CA - G4,OU=Symantec Trust Network,O=Symantec Corporation,C=US
    > GET / HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: www.baidu.com
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Accept-Ranges: bytes
    < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    < Connection: Keep-Alive
    < Content-Length: 2443
    < Content-Type: text/html
    < Date: Sun, 15 Apr 2018 14:28:10 GMT
    < Etag: "588603f3-98b"
    < Last-Modified: Mon, 23 Jan 2017 13:24:03 GMT
    < Pragma: no-cache
    < Server: bfe/1.0.8.18
    < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
    请求www.heboan.com/baidu01的过程

    详细查看www.heboan.com/baidu02过程

    root@c2 conf]# curl -vL www.heboan.com/baidu02
    * About to connect() to www.heboan.com port 80 (#0)
    *   Trying 192.168.88.2...
    * Connected to www.heboan.com (192.168.88.2) port 80 (#0)
    > GET /baidu02 HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: www.heboan.com
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.12.2
    < Date: Sun, 15 Apr 2018 14:34:47 GMT
    < Content-Type: text/html
    < Content-Length: 185
    < Connection: keep-alive
    < Location: https://www.baidu.com
    < 
    * Ignoring the response-body
    * Connection #0 to host www.heboan.com left intact
    * Issue another request to this URL: 'https://www.baidu.com'
    * About to connect() to www.baidu.com port 443 (#1)
    *   Trying 163.177.151.110...
    * Connected to www.baidu.com (163.177.151.110) port 443 (#1)
    * Initializing NSS with certpath: sql:/etc/pki/nssdb
    *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
      CApath: none
    * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    * Server certificate:
    *     subject: CN=baidu.com,OU=service operation department.,O="BeiJing Baidu Netcom Science Technology Co., Ltd",L=beijing,ST=beijing,C=CN
    *     start date: Jun 29 00:00:00 2017 GMT
    www.heboan.com/baidu02过程

    区别

    停止nginx服务
    [root@c2 conf]# /opt/nginx/sbin/nginx -s stop
    
    访问www.heboan.com/baidu01   (302)
        请求失败
    
    访问www.heboan.com/baidu02   (301)
        成功访问到百度
        
    原因:
    301 意味着客户端可以对结果进行缓存, 搜索引擎或者浏览器都可以把跳转后的地址缓存下来,下一次不必发送这个请求。 
    302 就是客户端必须请求原链接

    rewrite案例

    location / {
        rewrite ^/course-(d+)-(d+)-(d+).html$ /course/$1/$2/course_$3.html break;
        
        #if ($http_user_agent ~* Chrome){
        #    rewrite ^/nginx http://coding.imooc.com/class121.html break;
        #}
        
        #if (!-f $request_filename) {
        #    rewrite ~/(.*)$ http://www.imooc.com/$1 redirect;
        #}
    }
  • 相关阅读:
    leetcode643.滑动窗口例题
    BZOJ4195 离散化+并查集
    luogu线性表刷题
    2021-5-29 周报博客
    2021-5-28 日报博客
    2021-5-27 日报博客
    2021-5-26 日报博客
    2021-5-25 日报博客
    2021-5-24 日报博客
    梦断代码阅读笔记之二
  • 原文地址:https://www.cnblogs.com/sellsa/p/9347167.html
Copyright © 2020-2023  润新知