日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

nginx rewrite規(guī)則怎么配置?( 二 )


例如:
rewrite ^/test(.*)$ http://www.111cn.net/homepermanent;
訪問http://www.111cn.net/test?id=5 會跳轉(zhuǎn)到 http://www.111cn.net/home?id=5
例如:如果我們將類似URL /photo/123456 重定向到 /path/to/photo/12/1234/123456.png
Rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" /path/to/photo/$1/$1$2/$1$2$3.png ;
注:如果正則表達(dá)式里面有花括號"{"或"}" ,應(yīng)該使用雙引號或單引號 。
5.Set指令
語法:set variable value ; 默認(rèn)值:none ; 使用環(huán)境:server,location,if;
該指令用于定義一個變量,并給變量賦值 。變量的值可以為文本、變量以及文本變量的聯(lián)合 。
示例:set $varname "hello world";
6.Uninitialized_variable_warn指令
語法:uninitialized_variable_warn on|off
使用環(huán)境:http,server,location,if
該指令用于開啟和關(guān)閉未初始化變量的警告信息,默認(rèn)值為開啟 。
7.Nginx Rewrite可以用到的全局變量
$args ,$content_length ,$content_type ,$document_root ,$document_uri ,$host ,$http_user_agent ,$http_cookie ,$limit_rate ,$request_body_file ,$request_method ,$remote_addr ,$remote_port ,$remote_user ,$request_filename ,$request_uri ,$query_string ,$scheme ,$server_protocol ,$server_addr ,$server_name ,$server_port ,$uri
【nginx rewrite規(guī)則怎么配置?】====(詳情見附錄)====
Nginx的Rewrite規(guī)則編寫實例
1.當(dāng)訪問的文件和目錄不存在時,重定向到某個php文件
if( !-e $request_filename )
{
rewrite ^/(.*)$ index.php last;
}
2.目錄對換 /123456/xxxx====>/xxxx?id=123456
rewrite ^/(d+)/(.+)//$2?id=$1 last;
3.如果客戶端使用的是IE瀏覽器,則重定向到/ie目錄下
if( $http_user_agent~ MSIE)
{
rewrite ^(.*)$ /ie/$1 break;
}
4.禁止訪問多個目錄
location ~ ^/(cron|templates)/
{
deny all;
break;
}
5.禁止訪問以/data開頭的文件
location ~ ^/data
{
deny all;
}
6.禁止訪問以.sh,.flv,.mp3為文件后綴名的文件
location ~ .*.(sh|flv|mp3)$
{
return 403;
}
7.設(shè)置某些類型文件的瀏覽器緩存時間
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)$
{
expires 1h;
}
8.給favicon.ico和robots.txt設(shè)置過期時間;
這里為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;
}
9.設(shè)定某個文件的過期時間;這里為600秒,并不記錄訪問日志
location ^~ /html/scripts/loadhead_1.js {
access_logoff;
root /opt/lampp/htdocs/web;
expires 600;
break;
}
10.文件反盜鏈并設(shè)置過期時間
這里的return 412 為自定義的http狀態(tài)碼,默認(rèn)為403,方便找出正確的盜鏈的請求
“rewrite ^/ //img.ljsggw.cn/d/file/p/2022/11/16/18/leech.gif;”顯示一張防盜鏈圖片
“access_log off;”不記錄訪問日志,減輕壓力
“expires 3d”所有文件3天的瀏覽器緩存
location ~* ^.+.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ //img.ljsggw.cn/d/file/p/2022/11/16/18/leech.gif;
return 412;
break;
}
access_logoff;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
11.只充許固定ip訪問網(wǎng)站,并加上密碼
root/opt/htdocs/www;
allow208.97.167.194;
allow222.33.1.2;
allow231.152.49.4;
denyall;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;

推薦閱讀