複習Location規則順序,優先級演練;php
=/ 只會匹配/的狀況,優先級location/低html
=/index.html 只會匹配/inde.html狀況,優先級最高nginx
/ 匹配任何請求,優先級最低瀏覽器
/p_w_picpath/ 匹配任何/p_w_picpath路徑服務器
~* (.*)\.(html|jpg|png) 匹配html後綴的文件請求,可是優先級比上一個略低ide
localtion是nginx配置中的一個指令,用於訪問url匹配,而這個location中所配置的每一個指令將會啓動不一樣的模塊去完成相應的工做post
= 字符精確匹配 ^~ 最大前綴匹配 / 不帶任何前綴的最大前綴匹配 ~ 大小寫相關的正則匹配 ~* 大小寫無關的正則匹配 優化
匹配規則優先順序爲:(location=)>(location完整路徑)>(location^~路徑)>(location~*|~正則匹配)>(location部分起始路徑)>(location /)url
示例:spa
編寫20個Nginx Rewrite生產環境案例;
if($host = 'www.aaa.com' ){ rewrite ^/(.*)$ http://www.baidu.com/$1 permanent; }匹配域名爲www.aaa.com時,301 重定向到百度
rewrite ^/$ http://baidu.com/index01.html permenent; 方位此主機時,301重定向到baidu.com/index01.html
rewrite ^/jfedu/test01/$ /newindex.html last; 方位主機/jfedu/test01時,重定向到/newindex.html,瀏覽器地址不變
if($hosts != 'www.jfedu.net'){ rewrite ^/(.*)$ http://jfedu.net/$1 permenent;}多域名重定向,訪問域名不爲www.jfedu.net時,重定向到jfedu.net
if(! -e $request_filename){rewrite ^/(.*)$ /index.php last;}訪問不存在的文件或者目錄,重定向到index.php
rewrite ^/(.+)/(\d+) 、$1?id=$2 last;目錄對換,路由美化
if($http_user_agent ~ MSIE){rewrite ^/(.*)$ /ie/$1 break;}判斷瀏覽器類型
location ~* \.(sh|flv|mp3)$ {return 403} 禁止訪問以.sh、.flv、.mp3位文件名的文件
if($http_user_agent ~* "(Android)|(iPhone)|(Mobile)|(WAP)|(UCWECB)"){rewrite ^/$ http://m.jfedu.net/ permenent;} 將移動用戶訪問跳轉至移動端
if($args ~* tid=13){rewrite 404;}匹配訪問字符串跳轉
rewrite ^/([0-9]+)/jfedu/(.+)$ /index.php?tid/$1/items=$2 last; 訪問100690/jfedu/123 重定向至/index.php?tid/100690/items=123
rewrite ^/([a-zA-Z]+[\w\-]*)/([a-zA-Z]+[\w\-]*)$ /index.php?rr=$1/$2 last; 匹配到/user/admin-login跳轉到/index.php?rr=$1/$2
rewrite ^/([a-zA-Z]*)$ /$1.html last;
set $rewrite true; if($remote_addr = '192.168.111.126')}{set $rewrite false;} if($rewrite = true) { rewrite (.+) http://www.abc.com last; } 對特定的IP開啓訪問
location /post {rewrite (.+) http://www.abc.com/bbs$1 permenent; }將post下的訪問重定向到post下面
if($request_uri ~* ^/note\.php\?product_code=(.*)$ ){ rewrite (.*) http://abc.com/list permenent; } 將請求時/noet.php?product_code=123重定向到/list
location ~* /upload/.*\.php$ {return 404;}將upload下的ph請求返回404
if($request_uri ~* ^/form-(140|141)-(\d+)\.html$ ){rewrite (.* http://www.abc.com/list permenent;)} 將/form-401-5.html請求重定向到/list頁面
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/job_show_$3.html last; 將根目錄下的某個文件指向2級目錄
if($http_host ~* 「^(.*)\.i\.c1studio\.com$」){ rewrite ^(.*) http://top.jbxue.com; break } 三級域名跳轉,將www.a.i.c1stu.com跳轉top.jbxue.com
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time';
$remote_addr 記錄客戶端真實的IP地址 $server_name 虛擬主機名稱
$http_x_forward_for http請求的真實IP $remote_user 記錄客戶端用戶名稱
$request 記錄請求的url和HTTP協議 $status 記錄返回的http請求的狀態
$upstream_status upstream狀態 $ssl_protocal ssl協議版本
$body_bytes_sent 發送給客戶端的字節數,不包括相應頭的大小
$bytes_sent 發送給客戶端的總字節數 $connection_requests當前經過一個鏈接得到請求的數量
$http_referer 記錄從哪一個頁面鏈接訪問的 $http_user_agent記錄客戶端瀏覽器類型
$request_length 請求的長度,包括請求行,請求頭和請求正文
$msec 日誌寫入時間 $request_time程序響應時間
統計服務器獨立IP數 awk '{print $1}' access_log | sort -r | uniq -c | wc -l統計服務器PV量 awk '{print $7}' access.log |wc -l 統計服務器的UV awk '{print $11}' access.log | sort -r | uniq -c | wc -l分析nginx日誌截止目前爲止訪問量前20的IP awk '{print $11}' access.log | sort | uniq -c | sort -nr |head -20分析日誌早上9到12點的總請求量 sed -n '/2017:09:00/,/2017:12:00/'access.log 或者 awk '/2017:09:00/,/2017:12:00/' access.log | wc -l 分析日誌總的獨立IP數量 awl '{print $1}' access.log | sort | uniq -c | wc -l分析日誌截止目前爲止訪問量前20的IP列表 awk '{print $1}' access.log | sort | uniq -c | sort -nr | head 20分析日誌404 502 503 500 499等錯誤信息頁面,打印錯誤次數大於20的IP awk '{if($9~/502|499|500|502|503|504/)print $1,$9}' access.log | sort -uniq -c | sort -nr | awk '{if($1>20)print $2}'分析日誌訪問量最多的頁面 awk '{print $7}' acccess.log | sort | uniq -c | sort -nr |head 20分析日誌相應時間大於5秒的url,並打印出時間、URL、訪客IP awk '{if($NF>5)print $NF,$7,$1}' access.log | sort -nr