天天看點

Nginx中指令

Rewrite子產品

1 return指令

Syntax:	return code [text];
		return code URL;
		return URL;
Default:	—
Context:	server, location, if
           

傳回狀态碼

  • nginx自定義
    • 444:關閉連接配接
  • HTTP1.0标準
    • 301:http1.0永久重定向
    • 302:臨時重定向,禁止被緩存
  • HTTP1.1标準
    • 303:臨時重定向,允許改變方法,禁止被緩存
    • 307:臨時重定向,不允許改變方式,禁止被緩存
    • 308:永久重定向,不允許改變方法

示例

server {
    server_name wiki.clay-wangzhi.com;
    listen 8080;
    
    root html/;
    error_page 404/403.html;
    return 403;
    location / {
        return 404 "find nothing!";
    }
}
           

return指令與errror_page

Nginx中指令

2 rewrite指令

Syntax:	rewrite regex replacement [flag];
Default:	—
Context:	server, location, if
           

功能

  • 将regex指定的url替換成replacement這個新的url

    可以使用正規表達式及變量提取

  • 當replacement以http://或者https://或者$schema開頭,則直接傳回302重定向
  • 替換後的url根據flag指定的方式進行處理

    如果在同一級配置塊中存在多個rewrite規則,那麼會自下而下逐個檢查;被某條件規則替換完成後,會重新一輪的替換檢查,是以,隐含有循環機制;[flag]所表示的标志位用于控制此循環機制;

    • last:
      • 浏覽器位址欄URL位址不變
      • 用replacement這個URI進行新的location比對,重寫完成後停止對目前URI在目前location中後續的其它重寫操作,而後對新的URI啟動新一輪重寫檢查;提前重新開機新一輪循環;
    • beak:
      • break指令停止目前腳本指令的執行,等價于獨立的break指令,重寫完成後停止對目前URI在目前location中後續的其它重寫操作,而後直接跳轉至重寫規則配置塊之後的其它配置;結束循環;
    • redirect:
      • 浏覽器位址會顯示跳轉後的URL位址
      • 傳回302臨時重定向,
    • permanent:
      • 傳回301永久重定向
功能一句話概括為:使用nginx提供的全局變量或自己設定的變量,結合正規表達式和标志位實作url重寫以及重定向。

示例1

root html/;
        location /first {
            rewrite /first(.*) /second$1 last;
            return 200 'first!';
        }

        location /second {
            rewrite /second(.*) /third$1 break;
            return 200 'second!';
        }

        location /third {
            return 200 'third!';
        }
           

通路/first/1.txt的結果為:

html/third/1.txt

的内容

通路/second/1.txt的結果為:

html/third/1.txt

通路/third/1.txt的結果為:

third

示例2

# http://www.test.com/test/abc/1.html ⇒ http://www.test.com/ccc/bbb/2.html
location /test {
    rewrite .* /ccc/bbb/2.html permanent;
}

# http://www.test.com/2015/ccc/bbb/2.html ==> http://www.test.com/2014/ccc/bbb/2.html
location /2015 {
    rewrite ^/2015/(.*)$ /2014/$1 permanent;
}

# http://www.test.com/2015/ccc/bbb/2.html  ==> http://jd.com/index.php
location /2015 {
    if ($host ~* test.com) {
        rewrite .* http://www.jd.com/index.php permanent;
    }
}

# http://www.test.com/kkk/1.html ==> http://jd.com/kkk/1.html
location / {
    root html;
    index index.html index.htm;
    if ($host ~* test.com) {
        rewrite .* http://www.jd.com/$request_uri permanent;
    }
}

# 有時候在通路一個二級子目錄時會出現目錄後的/無法補齊,如:
#  elinks --dump www.test.com/www  無法通路
#  elinks --dump www.test.com/www/可以通路 
if (-d $request_filename) {
    rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
}
# ^/(.*)([^/])$表示以/符号開始并緊跟着任何字元,同時不是以/為結束的字元串,在我的url中,(.*)表示的ww,([^/])表示的w

# http://www.test.com/login/robin.html     ==>  http://www.test.com/reg/login.php?user=robin
location /login {
    rewrite ^/login/(.*)\.html$ /reg/login.php?user=$1 permanent;
}

# http://www.test.com/uplook/10-10-10.html  ==>  http://www.test.com/uplook/10/10/10.html 
location /uplook {
    rewrite ^/uplook/([0-9]+)-([0-9]+)-([0-9]+)\.html$ /uplook/$1/$2/$3.html permanent;
}

# set 指令是用于定義一個變量,并且指派。應用于server,location,if環境。
# http://robin.test.com ==> http://www.test.com/robin
# http://zorro.test.com ==> http://www.test.com/zorro
if ($host ~* "^www.test.com$") {
    break;
}
if ($host ~* "^(.*)\.test\.com$") {
    set $user $1;
    rewrite .* http://www.test.com/$user permanent;
}
           
last 一般寫在server和if中,而break一般使用在location中

3 if指令

Syntax:	if (condition) { ... }
Default:	—
Context:	server, location
           

規則:條件condition為真,則執行大括号内的指令;遵循值指令的繼承規則

if指令的條件表達式

  • 檢查變量為空或者值是否為0,直接使用
    • 當表達式隻是一個變量時,如果值為空或任何以 0 開頭的字元串都會當做 false
  • 将變量與字元串做比對,使用=或者!=
  • 将變量與正規表達式做比對
    • 大小寫敏感,或者!
    • 大小寫不敏感,*或者!*
  • 檢查檔案是否存在,使用-f或者!-f
  • 檢查目錄是否存在,使用-d或者!-d
  • 檢查檔案、目錄、軟連接配接是否存在,使用-e或者!-e
  • 檢查是否為可執行檔案,使用-x或者!-x
if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}
if ($repuest_method = POST) {
    return 405;
}
if ($slow) {
    limit_rate 10k;
}
if ($invalid_referer) {
    return 403;
}
           

4 rewrite_log指令

Syntax: rewrite_log on | off;
Default:    rewrite_log off;
Context:    http, server, location, if
           

本文版權歸作者所有,歡迎轉載,請務必添加原文連結。

繼續閱讀