文法規則: location [=|~|~*|^~] /uri/ { … }
= 開頭表示精確匹配
^~ 開頭表示uri以某個常規字串開頭,理解為匹配 url路徑即可。nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。
~ 開頭表示區分大小寫正則匹配
~* 開頭表示不區分大小寫正則匹配
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配 的正則
/ 通用匹配,任何請求都會匹配到。
多個location配置的情況下匹配順序為(參考資料而來,還未實際驗證,試試就知道了,不必拘泥,僅供參考):
首先匹配 =,其次匹配^~, 其次是按檔案中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。
例子,有如下匹配規則:
location = / { #規則A}location = /login { #規則B}location ^~ /static/ { #規則C}location ~ \.(gif|jpg|png|js|css)$ { #規則D}location ~* \.png$ { #規則E}location !~ \.xhtml$ { #規則F}location !~* \.xhtml$ { #規則G}location / { #規則H}
那麼產生的效果如下:
訪問根目錄/, 比如http://localhost/ 將匹配規則A
訪問 http://localhost/login 將匹配規則B,http://localhost/register 則匹配規則H
訪問 http://localhost/static/a.html 將匹配規則C
訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E,但是規則D順序優先,規則E不起作用,而 http://localhost/static/c.png 則優先匹配到 規則C
訪問 http://localhost/a.PNG 則匹配規則E, 而不會匹配規則D,因為規則E不區分大小寫。
訪問 http://localhost/a.xhtml 不會匹配規則F和規則G,http://localhost/a.XHTML不會匹配規則G,因為不區分大小寫。規則F,規則G屬於排除法,符合匹配規則但是不會匹配到,所以想想看實際應用中哪裡會用到。
訪問 http://localhost/category/id/1111 則最終匹配到規則H,因為以上規則都不匹配,這個時候應該是nginx轉寄請求給後端應用伺服器,比如FastCGI(php),tomcat(jsp),nginx作為方向Proxy 伺服器存在。
所以實際使用中,個人覺得至少有三個匹配規則定義,如下:
#直接匹配網站根,通過網域名稱訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。#這裡是直接轉寄給後端應用伺服器了,也可以是一個靜態首頁# 第一個必選規則location = / { proxy_pass http://tomcat:8080/index} # 第二個必選規則是處理靜態檔案請求,這是nginx作為http伺服器的強項# 有兩種配置模式,目錄匹配或尾碼匹配,任選其一或搭配使用location ^~ /static/ { root /webroot/static/;}location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/;} #第三個規則就是通用規則,用來轉寄動態請求到後端應用伺服器#非靜態檔案請求就預設是動態請求,自己根據實際把握#畢竟目前的一些架構的流行,帶.php,.jsp尾碼的情況很少了location / { proxy_pass http://tomcat:8080/}
未實驗過的其他資訊:
三、ReWrite文法
last – 基本上都用這個Flag。
break – 中止Rewirte,不在繼續匹配
redirect – 返回臨時重新導向的HTTP狀態302
permanent – 返回永久重新導向的HTTP狀態301
註:last和break最大的不同在於
- break是終止當前location的rewrite檢測,而且不再進行location匹配 - last是終止當前location的rewrite檢測,但會繼續重試location匹配並處理區塊中的rewrite規則
1、下面是可以用來判斷的運算式:
-f和!-f用來判斷是否存在檔案
-d和!-d用來判斷是否存在目錄
-e和!-e用來判斷是否存在檔案或目錄
-x和!-x用來判斷檔案是否可執行
2、下面是可以用作判斷的全域變數
$args #這個變數等於請求行中的參數。
$content_length #要求標頭中的Content-length欄位。
$content_type #要求標頭中的Content-Type欄位。
$document_root #當前請求在root指令中指定的值。
$host #請求主機頭欄位,否則為伺服器名稱。
$http_user_agent #用戶端agent資訊
$http_cookie #用戶端cookie資訊
$limit_rate #這個變數可以限制串連速率。
$request_body_file #用戶端請求主體資訊的臨時檔案名稱。
$request_method #用戶端請求的動作,通常為GET或POST。
$remote_addr #用戶端的IP地址。
$remote_port #用戶端的連接埠。
$remote_user #已經經過Auth Basic Module驗證的使用者名稱。
$request_filename #當前請求的檔案路徑,由root或alias指令與URI請求產生。
$query_string #與$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #請求使用的協議,通常是HTTP/1.0或HTTP/1.1。
$server_addr #伺服器位址,在完成一次系統調用後可以確定這個值。
$server_name #伺服器名稱。
$server_port #請求到達伺服器的連接埠號碼。
$request_uri #包含請求參數的原始URI,不包含主機名稱,如:”/foo/bar.php?arg=baz”。
$uri #不帶請求參數的當前URI,$uri不包含主機名稱,如”/foo/bar.html”。
$document_uri #與$uri相同。
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、Redirect文法
多目錄轉成參數
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
1. if ($host ~* (.*)\.domain\.com) {
2. set $sub_name $1;
3. rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
4. }
目錄對換
/123456/xxxx -> /xxxx?id=123456
1. rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
例如下面設定nginx在使用者使用ie的使用重新導向到/nginx-ie目錄下:
1. if ($http_user_agent ~ MSIE) {
2. rewrite ^(.*)$ /nginx-ie/$1 break;
3. }
目錄自動加“/”
1. if (-d $request_filename){
2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3. }
禁止htaccess
1. location ~/\.ht {
2. deny all;
3. }
禁止多個目錄
1. location ~ ^/(cron|templates)/ {
2. deny all;
3. break;
4. }
禁止以/data開頭的檔案
可以禁止/data/下多級目錄下.log.txt等請求;
1. location ~ ^/data {
2. deny all;
3. }
禁止單個目錄
不能禁止.log.txt能請求
1. location /searchword/cron/ {
2. deny all;
3. }
禁止單個檔案
1. location ~ /data/sql/data.sql {
2. deny all;
3. }
給favicon.ico和robots.txt設定到期時間;
這裡為favicon.ico為99 天,robots.txt為7天並不記錄404錯誤記錄檔
1. location ~(favicon.ico) {
2. log_not_found off;
3. expires 99d;
4. break;
5. }
6.
7. location ~(robots.txt) {
8. log_not_found off;
9. expires 7d;
10. break;
11. }
設定某個檔案的到期時間;這裡為600秒,並不記錄訪問日誌
1. location ^~ /html/scripts/loadhead_1.js {
2. access_log off;
3. root /opt/lampp/htdocs/web;
4. expires 600;
5. break;
6. }
檔案反盜鏈並設定到期時間
這裡的return 412 為自訂的http狀態代碼,預設為403,方便找出正確的盜鏈的請求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問日誌,減輕壓力
“expires 3d”所有檔案3天的瀏覽器緩衝
1. location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
2. valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
3. if ($invalid_referer) {
4. rewrite ^/ http://leech.c1gstudio.com/leech.gif;
5. return 412;
6. break;
7. }
8. access_log off;
9. root /opt/lampp/htdocs/web;
10. expires 3d;
11. break;
12. }
只充許固定ip訪問網站,並加上密碼
1. root /opt/htdocs/www;
2. allow 208.97.167.194;
3. allow 222.33.1.2;
4. allow 231.152.49.4;
5. deny all;
6. auth_basic "C1G_ADMIN";
7. auth_basic_user_file htpasswd;
將多級目錄下的檔案轉成一個檔案,增強seo效果
/job-123-456-789.html 指向/job/123/456/789.html
1. rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;
將根目錄下某個檔案夾指向2級目錄
如/shanghaijob/ 指向 /area/shanghai/
如果你將last改成permanent,那麼瀏覽器地址欄顯是 /location/shanghai/
1. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
上面例子有個問題是訪問/shanghai 時將不會匹配
1. rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
這樣/shanghai 也可以訪問了,但頁面中的相對連結無法使用,
如./list_1.html真真實位址是/area /shanghia/list_1.html會變成/list_1.html,導至無法訪問。
那我加上自動跳轉也是不行咯
(-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,所以沒有效果
1. if (-d $request_filename){
2. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3. }
知道原因後就好辦了,讓我手動跳轉吧
1. rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
2. rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
檔案和目錄不存在的時候重新導向:
1. if (!-e $request_filename) {
2. proxy_pass http://127.0.0.1;
3. }
網域名稱跳轉
1. server
2. {
3. listen 80;
4. server_name jump.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/ http://www.c1gstudio.com/;
8. access_log off;
9. }
多網域名稱轉向
1. server_name www.c1gstudio.com www.c1gstudio.net;
2. index index.html index.htm index.php;
3. root /opt/lampp/htdocs;
4. if ($host ~ "c1gstudio\.net") {
5. rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
6. }
第三層網域名跳轉
1. if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
2. rewrite ^(.*) http://top.yingjiesheng.com$1;
3. break;
4. }
網域名稱鏡向
1. server
2. {
3. listen 80;
4. server_name mirror.c1gstudio.com;
5. index index.html index.htm index.php;
6. root /opt/lampp/htdocs/www;
7. rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
8. access_log off;
9. }
以上就介紹了nginx之location配置,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。