1. 程式人生 > >Nginx的location詳解

Nginx的location詳解

web服務 nginx location

location的作用是根據用戶請求的URI來執行不同的應用。

官方提供的常見的location匹配語法:

location [ = | ~ | ~* | ^~ ] uri {

... ...

}

URI部分是關鍵,這個URI可以是普通的字符串地址路徑,或者是正則表達式,匹配成功則執行後面大括號裏的相關指令。

正則表達式的前面還可以有"~"或"~*"等特殊字符。

對location語法列表說明:

+--------+----------------------------+--------------+---------------------------------+

|location | [=|~|~*^~|@] | uri | {...} |

+--------+----------------------------+--------------+---------------------------------+

| 指令 | 匹配標識 |匹配的網站地址| 匹配URI後要執行的配置段 |

+--------+----------------------------+--------------+---------------------------------+

~用於區分大小寫(大小寫敏感)的匹配;

~*用於不區分大小寫的匹配;

!對匹配結果進行取反;

^~用來在進行常規的字符串匹配檢查之後,不做正則表達式的檢查,即如果最明確的那個字符串匹配的location配置有此前綴,那麽不做正則表達式的匹配。

location匹配示例:

location = / { #當用戶請求/時,將會匹配。

[ configueration A ]

}

location / { #當用戶請求/index.html時,將會匹配。

[ configueration B ]

}

location /document/ { #當用戶請求/document/doc.html時,將會匹配。

[ configueration C ]

}

location ^~/images/ { #當用戶請求/images/1.gif時,將會匹配。

[ configueration D ]

}

location ~* \.(gif|jpg|jpeg)$ { #當用戶請求/document/1.jpg時,將會匹配。

[ configueration E ]

}


location匹配實戰:

server {

listen 192.168.30.3;

server_name www.smartbro.com smart.com;

location / {

root html/www;

index index.html index.htm;

}

location = / {

root html/www;

index test01.html;

}

location /doc/ {

root html/www;

index test02.html;

}

location ^~ /images/ {

root html/www;

index test03.html;

}

access_log logs/access_www.log main;

}

echo ‘test01‘ > /application/nginx/html/www/test01.html #創建文件

echo ‘test02‘ > /application/nginx/html/www/test02.html

echo ‘test03‘ > /application/nginx/html/www/test03.html

mkdir /application/nginx/html/www/doc #創建目錄

mv /application/nginx/html/www/test02.html /application/nginx/html/www/doc #移動文件

ll /application/nginx/html/www/doc

total 4.0K

-rw-r--r-- 1 root root 7 Aug 11 17:01 test02.html #創建目錄

mkdir /application/nginx/html/www/images

mv /application/nginx/html/www/test03.html /application/nginx/html/www/images/ #移動文件

ll /application/nginx/html/www/images/

total 4.0K

-rw-r--r-- 1 root root 7 Aug 11 17:22 test03.html

/application/nginx/sbin/nginx -t #檢查語法

nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful

/application/nginx/sbin/nginx -s reload #平滑重啟Nginx

測試訪問:

curl http://192.168.30.3

test01

curl http://192.168.30.3/doc/

test02

curl http://192.168.30.3/images/

test03


--------------------------------------------------------------------------------------------------

Nginx rewrite:

--------------------------------------------------------------------------------------------------

主要實現URL地址的重寫。Nginx軟件的rewrite功能由pcre軟件的支持,就是通過Perl兼容正則表達式語法進行規則匹配。

默認的參數編譯的時候會安裝支持rewrite的模塊,但是必須要有pcre的支持。

rewrite 語法:

rewrite regex replacement [flag];

默認值:none

應用位置:server location if

rewrite是實現URL重寫的關鍵指令。

根據正則表達式的部分定向到placement部分,結束時flag標記。

regex常用表達式說明:

+---------------------------------------------------------------------------------------------------------------------+

| \ |將後面接著的字符標記為特殊字符或一個原義字符或一個向後引用 |

+---------------------------------------------------------------------------------------------------------------------+

| ^ |匹配輸入字符串的起始位置,如果設置了RegExp對象的Multiline屬性,^也匹配“\n”或“\r”之後的位置 |

+---------------------------------------------------------------------------------------------------------------------+

| $ |匹配輸入字符串的結束位置,如果設置了RegExp對象的Multiline屬性,$也匹配“\n”或“\r”之前的位置 |

+---------------------------------------------------------------------------------------------------------------------+

| * |匹配前面的字符零次或多次 |

+---------------------------------------------------------------------------------------------------------------------+

| + |匹配前面的字符一次或多次 |

+---------------------------------------------------------------------------------------------------------------------+

| ? |匹配前面的字符零次或一次,例如do(es)?既可以匹配do也可以匹配does中的do,等價於{0,1}。當該字 |

| |符緊跟在任何一個其他的限制符(* +? {n} {n,m})後面時,匹配模式是非貪婪模式的,非貪婪模式 |

| |會盡可能少的匹配所搜索的字符串,而默認的貪婪模式則會盡可能多的匹配所搜索的字符串,例如對於 |

| |字符串“oooo”,使用“o+?”會匹配單個字符o,而使用“o+”將會匹配所有的o |

+---------------------------------------------------------------------------------------------------------------------+

| . |匹配除去“\n”之外的任何單個字符,要匹配包括“\n”在內的任何字符,請使用“[.\n]”這樣的模式 |

+---------------------------------------------------------------------------------------------------------------------+

|(pattern) |匹配括號內的pattern,並可以在後面獲取對應的匹配,常用$0...$9屬性獲取小括號中匹配的內容 |

| |要匹配圓括號,使用“\(\)” |

+---------------------------------------------------------------------------------------------------------------------+


rewrite指令flag參數標記的說明:

+--------------------------------------------------------------------------------------------------+

| last | 本條規則匹配完成後,繼續向下匹配新的location URI規則 |

+--------------------------------------------------------------------------------------------------+

| break | 本條規則匹配及完成終止,不在繼續匹配後面的任何規則 |

+--------------------------------------------------------------------------------------------------+

| redirect | 返回302臨時重定向,瀏覽器地址欄會顯示跳轉後的URL地址 |

+--------------------------------------------------------------------------------------------------+

| permanent| 返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址 |

+--------------------------------------------------------------------------------------------------+

在flag標記中,last和break用來實現URL重寫,瀏覽器地址欄的URL地址不變,但在服務器訪問的應用程序和路徑發生了變化。

redirect和permanent用來實現URL跳轉,瀏覽器地址欄會顯示跳轉後的URL地址。

last和break標記實現的功能差不多一樣,但是在使用alias指令時必須使用last標記,使用proxy_pass指令時要使用break標記。

last標記在本條rewrite規則執行完畢後,會對其所在的server{...}標簽重新發起請求,而break標記會在本條規則匹配完成,終止匹配。


Nginx rewrite的企業應用場景:

--為了讓搜索引擎收錄網站的內容,並讓用戶體驗更好,企業會將動態URL地址偽靜態處理,以提供服務。

--網站換成新的域名後,讓舊的域名訪問跳轉到新的域名上,例如京東的360buy換成jd.com。

--根據特殊變量、目錄、客戶端的信息進行URL跳轉。


Nginx rewrite 301 跳轉:

除了使用別名的方式訪問同一個網站,還可使用301跳轉的方式實現。

vim /application/nginx/extra/www.conf

server {

listen 192.168.30.3;

server_name smartbro.com;

location / {

root html/www;

index index.html index.htm;

}

rewrite ^/(.*) http://www.smartbro.com/$1 permanent; #當訪問smartbro.com下面的所有內容的時候都會跳轉到www.smartbro.com

access_log logs/access_www.log main;

}

實現不同的域名的URI跳轉:

vim /application/nginx/extra/www.conf

server {

listen 192.168.30.3;

server_name www.smartbro.com;

location / {

root html/www;

index index.html index.htm;

}

if ( $http_host ~* "^(.*)\.smartbro\.com$" ) { #設置跳轉語句,不區分大小寫的正則匹配

set $domain $1;

rewrite ^(.*) http://www.smartbri.com/$domain/index.html break;

}

access_log logs/access_www.log main;

}

/application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.13.4//conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.13.4//conf/nginx.conf test is successful

/application/nginx/sbin/nginx -s reload

添加hosts解析:

vim /etc/hosts

192.168.10.3 www.smartbro.com bbs.smartbro.com pan.smartbro.com smartbro.com

使用瀏覽器訪問測試:

curl http://smartbro.com

Welcome to pan.smartbro.com

curl http://www.smartbro.com

Welcome to pan.smartbro.com


本文出自 “帥帥的小哥哥” 博客,請務必保留此出處http://xvjunjie.blog.51cto.com/12360960/1955619

Nginx的location詳解