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/html

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

訪問www.heboan.com/break/nginx

當訪問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/sql

當訪問www.heboan.com/last/,匹配到咱們定義location ~ ^/last而後根據rewrite規則,它會匹配到location /test/,除非它沒有匹配到location /test/,纔會去網站根目錄下去找test目錄json

[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;
    #}
}
相關文章
相關標籤/搜索