nginx rewrite實戰
前言
? Nginx的Rewrite規則和Apache的Rewite規則差別不是很大,幾乎可以直接使用(當然并不是說不改動任
何東西就拿來使用)。?
? 比如在Apache中這樣寫規則 rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last; 而在Nginx中寫
成這樣寫是無法啟動的,解決的辦法是加上兩個雙引號: rewrite “^/([0-9]{5}).html$” /viewthread.php?tid=$1
last;一般來說,nginx的rewrite規則可以寫在nginx配置文件中的location {}中,也可以針對特定的目錄進行location \demo {},這個實例就是針對服務器根目錄下的demo目錄的rewrite規則配置等等; nginx的rewrite重寫是基于pcre庫匹配的,所以會牽涉到一些基本的nginx匹配規則:?
實戰演示
nginx rewrite 正則表達式匹配
-
大小寫匹配
-
~ 為區分大小寫匹配
-
~* 為不區分大小寫匹配
-
!~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配
-
文件及目錄匹配
-
-f 和!-f用來判斷文件時否存在
-
-d和!-d用來判斷目錄是否存在
-
-e和!-e用來表示是否存在文件或者目錄
-
-x和!-x用來表示文件是否可執行
-
flag標記
-
last 相當于Apache里的[L]標記,表示完成rewrite
-
break 終止匹配, 不再匹配后面的規則。
-
redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址。
-
permanent 返回301永久重定向 地址欄會顯示跳轉后的地址。
-
一些可用的全局變量
1. $remote_addr //獲取客戶端ip
2. $binary_remote_addr //客戶端ip(二進制)
3. $remote_port //客戶端port,如:50472
4. $remote_user //已經經過Auth Basic Module驗證的用戶名
5. $host //請求主機頭字段,否則為服務器名稱,如:blog.sakmon.com
6. $request //用戶請求信息,如:GET ?a=1&b=2 HTTP/1.1
7. $request_filename //當前請求的文件的路徑名,由root或alias和URI request組合而成,如:/2013/81.html
8. $status //請求的響應狀態碼,如:200
9. $body_bytes_sent // 響應時送出的body字節數數量。即使連接中斷,這個數據也是精確的,如:40
10. $content_length // 等于請求行的“Content_Length”的值
11. $content_type // 等于請求行的“Content_Type”的值
12. $http_referer // 引用地址
13. $http_user_agent // 客戶端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
14. $args //與\$query_string相同 等于當中URL的參數(GET),如a=1&b=2
15. $document_uri //與\$uri相同 這個變量指當前的請求URI,不包括任何參數(見$args) 如:/2013/81.html
16. $document_root //針對當前請求的根路徑設置值
17. $hostname //如:centos53.localdomain
18. $http_cookie //客戶端cookie信息
19. $cookie_COOKIE //cookie COOKIE變量的值
20. $is_args //如果有$args參數,這個變量等于”?”,否則等于”",空值,如?
21. $limit_rate //這個變量可以限制連接速率,0表示不限速
22. $query_string // 與$args相同 等于當中URL的參數(GET),如a=1&b=2
23. $request_body // 記錄POST過來的數據信息
24. $request_body_file //客戶端請求主體信息的臨時文件名
25. $request_method //客戶端請求的動作,通常為GET或POST,如:GET
26. $request_uri //包含請求參數的原始URI,不包含主機名,如:/2013/81.html?a=1&b=2
27. $scheme //HTTP方法(如http,https),如:http
28. $uri //這個變量指當前的請求URI,不包括任何參數(見\$args) 如:/2013/81.html
29. $request_completion //如果請求結束,設置為OK. 當請求未結束或如果該請求不是請求鏈串的最后一個時,為空(Empty),如:OK
30. $server_protocol //請求使用的協議,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
31. $server_addr //服務器IP地址,在完成一次系統調用后可以確定這個值
32. $server_name //服務器名稱,如:blog.sakmon.com
33. $server_port //請求到達服務器的端口號,如:80
多目錄轉成參數
要求:abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?\$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
目錄對換
要求:/123456/xxxx -> /xxxx?id=123456
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
-
再來一個針對瀏覽器優化的自動rewrite,這里rewrite后的目錄可以是存在的
例如設定nginx在用戶使用IE瀏覽器的使用重定向到/nginx-IE目錄
規則如下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
目錄自動加“/” ,這個功能一般瀏覽器自動完成
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
禁止htaccess
location ~/\.ht {
deny all;
}
禁止多個目錄
location ~ ^/(cron|templates)/ {
deny all; break;
}
禁止以/data開頭的文件,可以禁止/data/下多級目錄下.log.txt等請求
location ~ ^/data {
deny all;
}
禁止單個文件
location ~ /data/sql/data.sql {
deny all;
}
給favicon.ico和robots.txt設置過期時間; 這里為favicon.ico為99天,robots.txt為7天并不記錄404錯誤日志
location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}
設定某個文件的瀏覽器緩存過期時間;這里為600秒,并不記錄訪問日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
expires 600;
break;
}
文件反盜鏈并設置過期時間–<盜鏈多次請求也會打開你的站點的圖片啊,所以設置下緩存時間,不會每次盜鏈都請求并下載這張圖片>
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.jjonline.cn *.jjonline.com.cn *.lanwei.org *.jjonline.org localhost 42.121.107.189;
if ($invalid_referer) {
rewrite ^/ http://img.jjonline.cn/forbid.gif;
return 417;
break;
}
access_log off;
break;
}
說明:
這里的return 417 為自定義的http狀態碼,默認為403,方便通過nginx的log文件找出正確的盜鏈的請求地址
“rewrite ^/ http://img.jjonline.cn/forbid.gif;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問日志,減輕壓力
“expires 3d”所有文件3天的瀏覽器緩存
-
只充許固定ip訪問網站,并加上密碼;這個對有權限認證的應用比較在行
location \ {
allow 22.27.164.25; #允許的ip
deny all;
auth_basic “KEY”; #認證的一些設置
auth_basic_user_file htpasswd;
}
說明:location的應用也有各種變化,這里的寫法就針對了根目錄了。
文件和目錄不存在的時重定向
if (!-e $request_filename) {
#proxy_pass http://127.0.0.1; #這里是跳轉到代理ip,這個代理ip上有一個監聽的web服務器
rewrite ^/ http://www.jjonline.cn/none.html; #跳轉到這個網頁去
#return 404; #直接返回404碼,然后會尋找root指定的404.html文件
}
域名跳轉
server {
listen 80;
server_name jump.jjonline.cn ;#需要跳轉的多級域名
index index.html index.htm index.php; #入口索引文件的名字
root /var/www/public_html/; #這個站點的根目錄
rewrite ^/ http://www.jjonline.cn/;
#rewrite到這個地址,功能表現:在瀏覽器上輸入jump.jjonline.cn并回車,不會有任何提示直接變成www.jjonline.cn
access_log off;
}
多域名轉向
server {
listen 80;
server_name www.jjonline.cn www.jjonline.org;
index index.html index.htm index.php;
root /var/www/public_html/;
if ($host ~ “jjonline\.org”) {
rewrite ^(.*) http://www.jjonline.cn$1 permanent;
}
}
三級域名跳轉
if ($http_host ~* “^(.*)\.i\.jjonline\.cn$”) {
rewrite ^(.*) http://demo.jjonline.cn$1;
break;
}
域名鏡向
server {
listen 80;
server_name mirror.jjonline.cn;
index index.html index.htm index.php;
root /var/www/public_html;
rewrite ^/(.*) http://www.jjonline.cn/$1 last;
access_log off;
}
某個子目錄作鏡向,這里的示例是demo子目錄
location ^~ /demo {
rewrite ^.+ http://demo.jjonline.cn/ last;
break;
}
請求URI不存在的重寫方法
location ~ {
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php last;
}
}
原創文章,作者:wanghui,如若轉載,請注明出處:http://www.www58058.com/59542