Nginx伪静态规则说明和Nginx常用Rewrite伪静态规则

迅恒数据中心

Nginx伪静态规则说明和常用Rewrite伪静态规则
正则表达式匹配,其中:
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
flag标记有:
last 相当于Apache里的[L]标记,表示完成rewrite
break 终止匹配, 不再匹配后面的规则
redirect 返回302临时重定向 地址栏会显示跳转后的地址
permanent 返回301永久重定向 地址栏会显示跳转后的地址


文件反盗链并设置过期时间:
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.gzsjzx.com *.xhisp.net localhost 1.1.1.1;
if ($invalid_referer) {
rewrite ^/ http://www.gzsjzx.com/xhisp.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
说明:
“rewrite ^/ http://www.gzsjzx.com/xhisp.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存

只允许固定ip访问网站,并加上密码:
root /opt/htdocs/www;
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;

文件和目录不存在的时候重定向:
if (!-e $request_filename) {
proxy_pass http://www.gzsjzx.com;
}

域名跳转:
server
{
listen 80;
server_name vip.xhisp.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.gzsjzx.com/;
access_log off;
}

多域名转向:
server_name www.xhisp.com www.xhisp.net;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ “c1gstudio/.net”) {
rewrite ^(.*) http://www.gzsjzx.com$1 permanent;
}

三级域名跳转:
if ($http_host ~* “^(.*)/.i/.xhisp/.net$”) {
rewrite ^(.*) http://www.gzsjzx.com$1;
break;
}

WordPress伪静态规则:
location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

PHPCMS伪静态规则:
location / {
###以下为PHPCMS 伪静态化rewrite法则
rewrite ^(.*)show-([0-9]+)-([0-9]+)/.html$ $1/show.php?itemid=$2&page=$3;
rewrite ^(.*)list-([0-9]+)-([0-9]+)/.html$ $1/list.php?catid=$2&page=$3;
rewrite ^(.*)show-([0-9]+)/.html$ $1/show.php?specialid=$2;
####以下为PHPWind 伪静态化rewrite法则
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9/_]+/.html)$ $1/simple/index.php?$2 last;
}

ECSHOP伪静态规则:
if (!-e $request_filename)
{
rewrite ^/index/.html /index.php last;
rewrite ^/category$ /index.php last;
rewrite ^/feed-c([0-9]+)/.xml$ /feed.php?cat=$1 last;
rewrite ^/feed-b([0-9]+)/.xml$ /feed.php?brand=$1 last;
rewrite ^/feed/.xml$ /feed.php last;
rewrite ^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)/.html$ /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;
rewrite ^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)/.html$ /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;
rewrite ^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)/.html$ /category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;
rewrite ^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)/.html$ /category.php?id=$1&brand=$2&page=$3 last;
rewrite ^/category-([0-9]+)-b([0-9]+)(.*)/.html$ /category.php?id=$1&brand=$2 last;
rewrite ^/category-([0-9]+)(.*)/.html$ /category.php?id=$1 last;
rewrite ^/goods-([0-9]+)(.*)/.html /goods.php?id=$1 last;
rewrite ^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)/.html$ /article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;
rewrite ^/article_cat-([0-9]+)-([0-9]+)(.*)/.html$ /article_cat.php?id=$1&page=$2 last;
rewrite ^/article_cat-([0-9]+)(.*)/.html$ /article_cat.php?id=$1 last;
rewrite ^/article-([0-9]+)(.*)/.html$ /article.php?id=$1 last;
rewrite ^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)/.html /brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;
rewrite ^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)/.html /brand.php?id=$1&cat=$2&page=$3 last;
rewrite ^/brand-([0-9]+)-c([0-9]+)(.*)/.html /brand.php?id=$1&cat=$2 last;
rewrite ^/brand-([0-9]+)(.*)/.html /brand.php?id=$1 last;
rewrite ^/tag-(.*)/.html /search.php?keywords=$1 last;
rewrite ^/snatch-([0-9]+)/.html$ /snatch.php?id=$1 last;
rewrite ^/group_buy-([0-9]+)/.html$ /group_buy.php?act=view&id=$1 last;
rewrite ^/auction-([0-9]+)/.html$ /auction.php?act=view&id=$1 last;
rewrite ^/exchange-id([0-9]+)(.*)/.html$ /exchange.php?id=$1&act=view last;
rewrite ^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)/.html$ /exchange.php?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last;
rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)/.html$ /exchange.php?cat_id=$1&page=$2&sort=$3&order=$4 last;
rewrite ^/exchange-([0-9]+)-([0-9]+)(.*)/.html$ /exchange.php?cat_id=$1&page=$2 last;
rewrite ^/exchange-([0-9]+)(.*)/.html$ /exchange.php?cat_id=$1 last;
}

SHOPEX伪静态规则 :
location / {
if (!-e $request_filename) {
rewrite ^/(.+/.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php?$1 last;
}
}

分类:教程帮助 百度收录 必应收录