nginx全局變量
rewrite實戰
nginx的location配置php
1、nginx全局變量css
https://github.com/aminglinux/nginx/blob/master/rewrite/variable.mdhtml
變量前端 |
說明linux |
$argsnginx |
請求中的參數,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2git |
$body_bytes_sentgithub |
服務器發送給客戶端的響應body字節數web |
$content_length後端 |
HTTP請求信息裏的"Content-Length" |
$conten_type |
HTTP請求信息裏的"Content-Type" |
$document_root |
nginx虛擬主機配置文件中的root參數對應的值 |
$document_uri |
當前請求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含後面的參數 |
$http_referer |
記錄這次請求是從哪一個鏈接訪問過來的,能夠根據該參數進行防盜鏈設置 |
$host |
主機頭,也就是域名 |
$http_user_agent |
客戶端的詳細信息,也就是瀏覽器的標識,用curl -A能夠指定 |
$http_cookie |
客戶端的cookie信息 |
$http_x_forwarded_for |
當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器也要進行相關的x_forwarded_for設置 |
$limit_rate |
若是nginx服務器使用limit_rate配置了顯示網絡速率,則會顯示,若是沒有設置, 則顯示0 |
$remote_addr |
客戶端的公網ip |
$remote_port |
客戶端的port |
$remote_user |
若是nginx有配置認證,該變量表明客戶端認證的用戶名 |
$request |
請求的URI和HTTP協議,如「GET /article-10000.html HTTP/1.1」 |
$request_body_file |
作反向代理時發給後端服務器的本地資源的名稱 |
$request_method |
請求資源的方式,GET/PUT/DELETE等 |
$request_filename |
當前請求的資源文件的路徑名稱,至關因而$document_root/$document_uri的組合 |
$request_uri |
請求的連接,包括$document_uri和$args |
$scheme |
請求的協議,如ftp,http,https |
$server_protocol |
客戶端請求資源使用的協議的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等 |
$server_addr |
服務器IP地址 |
$server_name |
服務器的主機名 |
$server_port |
服務器的端口號 |
$status |
http狀態碼,記錄請求返回的狀態碼,例如:200、30一、404等 |
$uri |
和$document_uri相同 |
$http_referer |
客戶端請求時的referer,通俗講就是該請求是經過哪一個連接跳過來的,用curl -e能夠指定 |
$time_local |
記錄訪問時間與時區,如18/Jul/2014:17:00:01 +0800 |
2、rewrite實戰
域名跳轉(域名重定向)
示例1(不帶條件的):
server{ listen 80; server_name www.a.com; root /data/wwwroot/www.a.com; index index.html; rewrite /(.*) http://www.b.com/$1 permanent; }
訪問a.com跳轉到b.com
示例2(帶條件的):
server{ listen 80; server_name www.a.com a.com; root /data/wwwroot/www.a.com; index index.html; if ($host != 'www.a.com') { rewrite /(.*) http://www.a.com/$1 permanent; } }
經過判斷條件,若是$host不等於www.a.com的,跳轉到www.a.com
示例3(http跳轉到https):
server{ listen 80; server_name www.a.com; root /data/wwwroot/www.a.com; index index.html; rewrite /(.*) https://www.a.com/$1 permanent; }
示例4(域名訪問二級目錄)
server{ listen 80; server_name blog.a.com; index index.html; rewrite /(.*) http://www.a.com/blog/$1 permanent; }
示例5(靜態請求分離)
server{ listen 80; server_name www.a.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js)$ { rewrite /(.*) http://img.a.com/$1 permanent; } } 或者: server{ listen 80; server_name www.a.com; index index.html; if ( $uri ~* 'jpg|jpeg|gif|css|png|js$') { rewrite /(.*) http://img.a.com/$1 permanent; } }
防盜鏈
示例6
server{ listen 80; server_name www.a.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.a.com a.com *.tobe.com tobe.com; if ($invalid_referer) { rewrite /(.*) http://img.a.com/images/forbidden.png; } } ....... }
說明:*這裏是通配,跟正則裏面的*不是一個意思,valid_referers定義白名單,none指的是referer不存在、爲空的狀況(curl -e 測試),
blocked指的是referer頭部的值被防火牆或者代理服務器刪除或者假裝的狀況,
該狀況下,referer頭部的值不以http://或者https://開頭(curl -e 後面跟的referer不以http://或者https://開頭)。
或者:
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.a.com *.tobe.com a.com tobe.com; if ($invalid_referer) { return 403; } }
僞靜態
示例7(discuz僞靜態):
location / { rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last; }
rewrite多個條件的而且
示例8:
location /{ set $rule 0; #值爲0 if ($document_uri !~ '^/abc') #!~不匹配以abc開頭的 { set $rule "${rule}1"; #01 } if ($http_user_agent ~* 'ie6|firefox') { set $rule "${rule}2"; #012 } if ($rule = "012") { rewrite /(.*) /abc/$1 redirect; } }
當知足兩個條件不以abc開頭,而且http_user_agent爲ie6或firefox時,跳轉到http://www.a.com/aaa/,不知足其中的一個條件時,返回了404
3、nginx的location配置
安裝第三方模塊echo-nginx-module
cd /usr/local/src/ git clone https://github.com/openresty/echo-nginx-module.git
nginx編譯安裝後的操做:
cd /usr/local/src/nginx-1.16.1
從新編譯
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module make && make install
/usr/local/nginx/sbin/nginx -V
測試中使用,能夠在虛擬主機配置文件中直接使用,如:echo 123;
location語法:
nginx location語法規則:location [=|~|~*|^~] /uri/ { … } nginx的location匹配的變量是$uri
符號 | 說明 |
---|---|
= | 表示精確匹配 |
^~ | 表示uri以指定字符或字符串開頭 |
~ | 表示區分大小寫的正則匹配 |
~* | 表示不區分大小寫的正則匹配 |
/ | 通用匹配,任何請求都會匹配到 |
規則優先級:
= 高於 ^~ 高於 ~* 等於 ~ 高於 /
規則示例:
location = "/12.jpg" { ... } 如: www.a.com/12.jpg 匹配 www.a.com/abc/12.jpg 不匹配 location ^~ "/abc/" { ... } 如: www.a.com/abc/123.html 匹配 www.a.com/a/abc/123.jpg 不匹配 location ~ "png" { ... } 如: www.a.com/aaa/bbb/ccc/123.png 匹配 www.a.com/aaa/png/123.html 匹配 location ~* "png" { ... } 如: www.a.com/aaa/bbb/ccc/123.PNG 匹配 www.a.com/aaa/png/123.html 匹配 location /admin/ { ... } 如: www.a.com/admin/aaa/1.php 匹配 www.a.com/123/admin/1.php 不匹配
小常識:
有些資料上介紹location支持不匹配 !~, 如: location !~ 'png'{ ... } 這是錯誤的,location不支持 !~ 若是有這樣的需求,能夠經過if來實現, 如: if ($uri !~ 'png') { ... } 注意:location優先級小於if