nginx配置文件rewrite和if rewrite 語法:rewrite regex replacement flag;,如: rewrite ^/images/(.*\.jpg)$ /imgs/$1 break; 此處的$1用於引用(.*.jpg)匹配到的內容,又如: rewrite ^/bb ...
nginx配置文件rewrite和if
rewrite
語法:rewrite regex replacement flag;,如:
rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;
此處的$1用於引用(.*.jpg)匹配到的內容,又如:
rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;
如上例所示,replacement可以是某個路徑,也可以是某個URL
常見的flag
flag | 作用 |
---|---|
last | 基本上都用這個flag,表示當前的匹配結束,繼續下一個匹配,最多匹配10個到20個 一旦此rewrite規則重寫完成後,就不再被後面其它的rewrite規則進行處理 而是由UserAgent重新對重寫後的URL再一次發起請求,並從頭開始執行類似的過程 |
break | 中止Rewrite,不再繼續匹配 一旦此rewrite規則重寫完成後,由UserAgent對新的URL重新發起請求, 且不再會被當前location內的任何rewrite規則所檢查 |
redirect | 以臨時重定向的HTTP狀態302返回新的URL |
permanent | 以永久重定向的HTTP狀態301返回新的URL |
rewrite模塊的作用是用來執行URL重定向。這個機制有利於去掉惡意訪問的url,也有利於搜索引擎優化(SEO)
nginx使用的語法源於Perl相容正則表達式(PCRE)庫,基本語法如下:
標識符 | 意義 |
---|---|
^ | 必須以^後的實體開頭 |
$ | 必須以$前的實體結尾 |
. | 匹配任意字元 |
[] | 匹配指定字元集內的任意字元 |
[^] | 匹配任何不包括在指定字元集內的任意字元串 |
| | 匹配 | 之前或之後的實體 |
() | 分組,組成一組用於匹配的實體,通常會有 | 來協助 |
//創建images目錄,用於存放圖片
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir images
//images中存放一張圖片
[root@nginx html]# ls images/
1.jpg
//修改配置文件添加location
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# vim nginx.conf
location /images {
root html;
}
[root@nginx conf]# systemctl restart nginx
瀏覽器訪問
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# ls
50x.html abc images index.html
[root@nginx html]# mv images imgs //正常情況下當修改了目錄名 就不能
[root@nginx html]# ls
50x.html abc imgs index.html
[root@nginx conf]# pwd
/usr/local/nginx/conf
[root@nginx conf]# vim nginx.conf
location /images {
rewrite ^/images/(.*)$ /imgs/$1 break; //所有以images開頭訪問的 用imgs響應
}
[root@nginx conf]# systemctl restart nginx
瀏覽器訪問images/1.jpg
使用imgs/1.jpg也可以訪問
[root@nginx conf]# vim nginx.conf
location /images {
rewrite ^/images/(.*\.jpg)$ https://t7.baidu.com/it/u=2511982910,2454873241&fm=193&f=GIF break; //將響應換為網頁圖片的地址
}
[root@nginx conf]# systemctl restart nginx
瀏覽器訪問images/1.jpg 跳轉到了網頁圖片
當前匹配結束 繼續匹配下一個
[root@nginx conf]# vim nginx.conf
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 last;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com break;
}
[root@nginx conf]# systemctl restart nginx
訪問images/1.jpg 進行下一個匹配 跳轉到百度
redirect臨時重定向
[root@nginx conf]# vim nginx.conf
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 redirect;
}
[root@nginx conf]# systemctl restart nginx
permanent永久重定向
[root@nginx conf]# vim nginx.conf
location /images {
rewrite ^/images/(.*\.jpg)$ /imgs/$1 permanent;
}
location /imgs {
rewrite ^/imgs/(.*\.jpg)$ http://www.baidu.com break;
}
[root@nginx conf]# systemctl restart nginx
if
語法:if (condition) {…}
應用場景:
- server段
- location段
常見的condition
- 變數名(變數值為空串,或者以“0”開始,則為false,其它的均為true)
- 以變數為操作數構成的比較表達式(可使用=,!=類似的比較操作符進行測試)
- 正則表達式的模式匹配操作
- ~:區分大小寫的模式匹配檢查
- ~*:不區分大小寫的模式匹配檢查
- !和!*:對上面兩種測試取反
- 測試指定路徑為文件的可能性(-f,!-f)
- 測試指定路徑為目錄的可能性(-d,!-d)
- 測試文件的存在性(-e,!-e)
- 檢查文件是否有執行許可權(-x,!-x)
基於瀏覽器實現分離案例
if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_user_agent ~ Chrome) {
rewrite ^(.*)$ /chrome/$1 break;
}
防盜鏈案例
location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.wxh.world; //只有從頁面點擊的圖片是有效鏈接
if ($invalid_referer) {
rewrite ^/ http://www.baidu.coml; //無效鏈接直接從寫URL到百度
}
}