1、nginx rewrite规则范例1
server { listen 443; server_name v.test.com; access_log /opt/logs/tengine_ssl/v.test.com.access.log ssl; error_log /opt/logs/tengine_ssl/v.test.com.error.log warn; root /opt/case/v.test.com; index index.php index.html index.htm; ssl on; ssl_certificate /opt/app/tengine/conf/ssl/test.com.pem; ssl_certificate_key /opt/app/tengine/conf/ssl/test.com.key; location / { rewrite "^/tv/([^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}-?[^-]{0,}-?\d{0,}\.html)$" /moviecore/server/list/index.php?ctl=tvList&args=$1 last; rewrite "^/dongman/([^-]{0,}-[^-]{0,}-\d{0,}-\d{0,}-[^-]{0,}-\d{0,}\.html)$" /moviecore/server/list/index.php?ctl=dmList&args=$1 last; rewrite "^/zongyi/([^-]{0,}-[^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}\.html)$" /moviecore/server/list/index.php?ctl=zyList&args=$1 last; rewrite "^/dianying/([^-]{0,}-[^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}-[^-]{0,}-[^-]{0,}-?\d{0,}.html)$" /moviecore/server/list/index.php?ctl=dyList&args=$1 last; rewrite "^/tv/(\d{1,6})\.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=tv&id=$1 last; rewrite "^/dongman/(\d{1,6})\.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=dm&id=$1 last; rewrite "^/dianying/(\d{1,6}).html$" /moviecore/server/detail/index.php?ctl=dyDetail&id=$1 last; rewrite "^/zongyi/(\d{1,6})\.html$" /moviecore/server/detail/index.php?ctl=zyDetail&id=$1 last; rewrite "^/dianying/(\d{1,6})/1_0?.html$" /moviecore/server/detail/index.php?ctl=dyDetail&act=play&id=$1&cid=0 last; rewrite "^/zongyi/(\d{1,6})/1_0?.html$" /moviecore/server/detail/index.php?ctl=zyDetail&act=play&id=$1&cid=0 last; rewrite "^/(dianying|tv|dongman|zongyi)/ping/(\d{1,6})\.html$" /moviecore/server/comment/index.php?id=$2&media=$1 last; rewrite "^/tv/(\d{1,6})/2(_(\d{1,4}))?.html$" /moviecore/server/drama/index.php?type=juqing&tv_id=$1&op=generate&num=$3 last; rewrite "^/tv/(\d{1,6})/3.html$" /moviecore/server/drama/index.php?type=yanyuan&tv_id=$1&op=generate last; rewrite "^/dianying/(\d{1,6})/1.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=1&mediaType=dy&mediaId=$1 last; rewrite "^/tv/(\d{1,6})/1_0?.html$" /moviecore/server/detail/index.php?ctl=newDetail&act=play&media=tv&id=$1&cid=0 last; rewrite "^/dongman/(\d{1,6})/1_0?.html$" /moviecore/server/detail/index.php?ctl=newDetail&act=play&media=dm&id=$1&cid=0 last; rewrite "^/zongyi/(\d{1,6})/1.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=1&mediaType=zy&mediaId=$1 last; rewrite "^/dianying/(\d{1,6})/4.html$" /moviecore/server/detail/index.php?ctl=dyDetail&act=stills&id=$1 last; ###0630# rewrite "^/dianying/(\d{1,6})/2.html$" /moviecore/server/detail/index.php?ctl=dyDetail&act=word&acttype=plots&id=$1 last; rewrite "^/dianying/(\d{1,6})/5.html$" /moviecore/server/detail/index.php?ctl=dyDetail&act=word&acttype=behind_story&id=$1 last; rewrite "^/dianying/(\d{1,6})/6.html$" /moviecore/server/detail/index.php?ctl=dyDetail&act=word&acttype=dialogue&id=$1 last; #0710 rewrite "^/(\w+)/dianying.html$" /moviecore/server/list/index.php?ctl=dyList&act=star&name=$1 last; rewrite "^/(\w+)/dianshiju.html$" /moviecore/server/list/index.php?ctl=tvList&act=star&name=$1 last; ##0719 rewrite "^/tv/(\d{1,6})/4.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=tv&act=stills&id=$1 last; rewrite "^/tv/(\d{1,6})/5.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=tv&act=word&acttype=behind_story&id=$1 last; rewrite "^/tv/(\d{1,6})/6.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=tv&act=word&acttype=dialogue&id=$1 last; rewrite "^/dongman/(\d{1,6})/4.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=dm&act=stills&id=$1 last; rewrite "^/dongman/(\d{1,6})/2.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=dm&act=word&acttype=plots&id=$1 last; rewrite "^/dongman/(\d{1,6})/5.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=dm&act=word&acttype=behind_story&id=$1 last; rewrite "^/dongman/(\d{1,6})/6.html$" /moviecore/server/detail/index.php?ctl=newDetail&media=dm&act=word&acttype=dialogue&id=$1 last; rewrite "^/zongyi/(\d{1,6})/4.html$" /moviecore/server/detail/index.php?ctl=zyDetail&act=stills&id=$1 last; rewrite "^/zongyi/(\d{1,6})/2.html$" /moviecore/server/detail/index.php?ctl=zyDetail&act=word&acttype=plots&id=$1 last; rewrite "^/zongyi/(\d{1,6})/5.html$" /moviecore/server/detail/index.php?ctl=zyDetail&act=word&acttype=behind_story&id=$1 last; rewrite "^/zongyi/(\d{1,6})/6.html$" /moviecore/server/detail/index.php?ctl=zyDetail&act=word&acttype=dialogue&id=$1 last; #PC看点啥详情页 rewrite "^/kandian/(\d{1,6}).html$" /moviecore/server/detail/index.php?ctl=kdsPcDetailV1&id=$1 last; #2018-05-16 PC端看点啥列表页 rewrite "^/kds/(\w+)-(\w+){0,}.html$" /moviecore/server/list/index.php?ctl=kdsList&cate=$1&tag=$2 last; #2018-05-16 PC端看点啥短视频频道页 rewrite "^/kds(/\w+|)(/|)$" /moviecore/server/index/index.php?ctl=kdsPcIndex&identity=$1 last; #pc短视频详情页 rewrite "^/tv/(\d{1,6})/1.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=1&mediaType=tv&mediaId=$1 last; rewrite "^/dongman/(\d{1,6})/1.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=1&mediaType=dm&mediaId=$1 last; rewrite "^/zongyi/(\d{1,6})/1(_(\d{1,10}))?.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=$3&mediaType=zy&mediaId=$1 last; rewrite "^/dianying/(\d{1,6})/1(_(\d{1,10}))?.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=$3&mediaType=dy&mediaId=$1 last; rewrite "^/tv/(\d{1,6})/1(_(\d{1,10}))?.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=$3&mediaType=tv&mediaId=$1 last; rewrite "^/dongman/(\d{1,6})/1(_(\d{1,10}))?.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=$3&mediaType=dm&mediaId=$1 last; rewrite "^/shipin/(\d{1,10})?.html$" /moviecore/server/detail/index.php?ctl=svDetailV1&id=$1 last; #PC看点啥作者页 rewrite "^/kds/author/(\d{1,6}).html$" /moviecore/server/kdsPc/index.php?ctl=kdsPcAuthor&id=$1 last; #PC看点啥专辑页 rewrite "^/kds/album/(\d{1,6}).html$" /moviecore/server/kdsPc/index.php?ctl=kdsPcSpecial&type=album&sid=$1 last; #PC看点啥专题页 rewrite "^/kds/zt/(\d{1,6}).html$" /moviecore/server/kdsPc/index.php?ctl=kdsPcSpecial&type=zt&sid=$1 last; #PC看点啥标签页 rewrite "^/kds/tag/(\d{1,6}).html$" /moviecore/server/kdsPc/index.php?ctl=kdsPcTag&id=$1 last; rewrite "^/yingxun/?$" /dianying/yingxun.html last; } location ~ \.php { try_files $uri = /moviecore/404show.php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } error_page 404 = /moviecore/404show.php; }
2、nginx rewrite 规则范例2
server { listen 443; server_name v2.test.com; access_log /opt/logs/tengine_ssl/test.com.access.log ssl; error_log /opt/logs/tengine_ssl/test.com.error.log warn; root /opt/case/test2.com; index index.php index.html index.htm; ssl on; ssl_certificate /opt/app/tengine/conf/ssl/test.com.pem; ssl_certificate_key /opt/app/tengine/conf/ssl/test.com.key; location = /dianying/ { rewrite "^/dianying/?(.*)?$" /moviecore/server/mversionV1/index.php?ctl=showIndex&channel=dy last; } location = /tv/ { rewrite "^/tv/?(.*)?$" /moviecore/server/mversionV1/index.php?ctl=showIndex&channel=tv last; } location = /dongman/ { rewrite "^/dongman/?(.*)?$" /moviecore/server/mversionV1/index.php?ctl=showIndex&channel=dm last; } location = /zongyi/ { rewrite "^/zongyi/?(.*)?$" /moviecore/server/mversionV1/index.php?ctl=showIndex&channel=zy last; } location = / { rewrite "^/?(.*)?$" /moviecore/server/mversionV1/index.php?ctl=showIndex&channel=default last; } location / { rewrite "^/dianying/news/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&act=getMoreNews&type=dy&mediaId=$1; rewrite "^/tv/news/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&act=getMoreNews&type=tv&mediaId=$1; rewrite "^/dongman/news/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&act=getMoreNews&type=dongman&mediaId=$1; if ( $query_string ~ "dy=(\d{1,10})") { set $mediaId $1; rewrite "^/news/(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&act=newsDetail&id=$1&mediaId=$mediaId last; } if ( $query_string ~ "tv=(\d{1,10})") { set $mediaId $1; rewrite "^/news/(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&act=newsDetail&id=$1&mediaId=$mediaId&type=tv last; } rewrite "^/news/(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&act=newsDetail&id=$1 last; rewrite "^/zongyi/ping/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=zyDetailNew&id=$1&pageType=commit last; rewrite "^/$" /moviecore/server/mversionV1/index.php?ctl=indexManage last; if ( $query_string ~* (.*) ){ set $order $1; rewrite "^/tv/([^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}-?[^-]{0,}-?\d{0,}\.html)$" /moviecore/server/mversion/index.php?ctl=tvList&args=$1&order=$order last; rewrite "^/dongman/([^-]{0,}-[^-]{0,}-\d{0,}-\d{0,}-[^-]{0,}-\d{0,}\.html)$" /moviecore/server/mversion/index.php?ctl=dmList&args=$1&order=$order last; rewrite "^/zongyi/([^-]{0,}-[^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}\.html)$" /moviecore/server/mversion/index.php?ctl=zyList&args=$1&order=$order last; } rewrite "^/dianying/news/(\d{1,10})_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&act=newsDetail&&id=$1&mediaId=$2 last; rewrite "^/tv/news/(\d{1,10})_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&act=newsDetail&id=$1&mediaId=$2&type=tv last; rewrite "^/tv/role/(\d{1,9}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&pageType=role&id=$1&media=tv last; rewrite "^/tv/picture/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&id=$1&pageType=picture&media=tv last; rewrite "^/tv/review/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&id=$1&pageType=review&media=tv last; rewrite "^/tv/article/(\d{1,9}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&act=reviewDetail&id=$1&media=tv last; rewrite "^/dianying/article/(\d{1,9}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&act=reviewDetail&id=$1 last; rewrite "^/dianying/picture/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1&pageType=picture last; rewrite "^/dianying/role/(\d{1,9}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&pageType=role&id=$1 last; rewrite "^/dianying/review/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1&pageType=review last; rewrite "^/dianying/(\d{1,6})/1_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=shortVideo&id=$1&sid=$2&type=dy last; rewrite "^/tv/(\d{1,6})/1_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=shortVideo&id=$1&sid=$2&type=tv last; rewrite "^/dongman/(\d{1,6})/1_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=shortVideo&id=$1&sid=$2&type=dm last; rewrite "^/zongyi/(\d{1,6})/1_(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=shortVideo&id=$1&sid=$2&type=zy last; rewrite "^/dianying/yanyuan/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1&pageType=actor last; rewrite "^/dianying/ping/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1&pageType=commit last; rewrite "^/dianying/related/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1&pageType=connect last; rewrite "^/tv/yanyuan/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1&pageType=yanyuan last; rewrite "^/juqing/(\d{1,6})_(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1&pageType=juqing&eid=$2 last; rewrite "^/juqing/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1&pageType=juqing&eid=1 last; rewrite "^/tv/ping/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1&pageType=ping last; rewrite "^/tv/related/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1&pageType=related last; rewrite "^/dongman/ping/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=dm&id=$1&pageType=ping last; rewrite "^/dongman/related/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=dm&id=$1&pageType=related last; rewrite "^/dianying/([^-]{0,}-[^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}-[^-]{0,}-[^-]{0,}-?\d{0,}.html)$" /moviecore/server/mversion/index.php?ctl=dyList&args=$1 last; rewrite "^/tv/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=tv&id=$1 last; rewrite "^/dongman/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&media=dm&id=$1 last; rewrite "^/dianying/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetail&id=$1 last; rewrite "^/zongyi/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=zyDetailNew&id=$1 last; rewrite "^/play/href=(.*)$" /moviecore/server/mversion/index.php?ctl=play&href=$1 last; rewrite "^/playapp/href=(.*)$" /moviecore/server/mversion/index.php?ctl=play&act=playApp&href=$1 last; rewrite "^/app/dianying/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=dyDetailApp&id=$1 last; rewrite "^/app/tv/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailApp&media=tv&id=$1 last; rewrite "^/app/dongman/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailApp&media=dm&id=$1 last; rewrite "^/app/zongyi/(\d{1,6}).html$" /moviecore/server/mversion/index.php?ctl=zyDetailApp&id=$1 last; rewrite "^/dianying/(\d{1,6})/1.html$" /moviecore/server/mversion/index.php?ctl=dyDetailV2&id=$1&cid=1&act=play last; rewrite "^/tv/(\d{1,6})/(\d{1,4}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&id=$1&cid=$2&media=tv&act=play last; rewrite "^/dongman/(\d{1,6})/(\d{1,4}).html$" /moviecore/server/mversion/index.php?ctl=tvDmDetailV2&id=$1&cid=$2&media=dm&act=play last; rewrite "^/(dianying|tv|dongman|zongyi|yule|zt|(dianying|tv|dongman|zongyi)/top|dianying/xinpian/all)$" /$1/ permanent; rewrite "^/zongyi/(\d{1,6})/(\d{1}|\d{8}).html$" /moviecore/server/mversion/index.php?ctl=zyDetailNew&id=$1&cid=$2&act=play last; #2017-09-20 M版周边页面-电影 rewrite "^/dianying/(\d{1,6})/4.html$" /moviecore/server/mversion/index.php?ctl=dyDetailV2&act=stills&id=$1 last; rewrite "^/dianying/(\d{1,6})/2.html$" /moviecore/server/mversion/index.php?ctl=dyDetailV2&act=word&acttype=plots&id=$1 last; rewrite "^/dianying/(\d{1,6})/5.html$" /moviecore/server/mversion/index.php?ctl=dyDetailV2&act=word&acttype=behind_story&id=$1 last; rewrite "^/dianying/(\d{1,6})/6.html$" /moviecore/server/mversion/index.php?ctl=dyDetailV2&act=word&acttype=dialogue&id=$1 last; rewrite "^/zixun/(\d{1,10}).html$" /moviecore/server/mversion/index.php?ctl=shortVideo&sid=$1&type=zx last; rewrite "^/tv/?(.*)?$" /m/tv/$1 last; rewrite "^/dongman/?(.*)?$" /m/dongman/$1 last; rewrite "^/dianying/?(.*)?$" /m/dianying/$1 last; rewrite "^/zongyi/?(.*)?$" /m/zongyi/$1 last; rewrite "^/zt/?(.*)?$" /m/zt/$1 last; rewrite "^/zhibo/?(.*)?$" /m/zhibo/$1 last; rewrite "^/juqing/?(.*)?$" /m/juqing/$1 last; rewrite "^/new/?(.*)?$" /m/new/$1 last; rewrite "^/?(\w{0,}.html)?$" /m/$1 last; rewrite "^/kandian/(\d{1,6}).html$" /moviecore/server/kds/index.php?ctl=detail&id=$1 last; # rewrite "^/kds(/\w+|)(/|)$" /moviecore/server/kds/index.php?ctl=kdsIndex&act=index&type=$1 last; #看点啥作者页 rewrite "^/kds/author/(\d{1,6}).html$" /moviecore/server/kds/index.php?ctl=kdsAuthor&id=$1 last; #看点啥专辑页 rewrite "^/kds/album/(\d{1,6}).html$" /moviecore/server/kds/index.php?ctl=kdsAlbum&id=$1 last; #看点啥标签页 rewrite "^/kds/tag/(\d{1,6}).html$" /moviecore/server/kds/index.php?ctl=kdsTag&id=$1 last; #看点啥专题页 rewrite "^/kds/zt/(\d{1,6}).html$" /moviecore/server/kds/index.php?ctl=kdsSpecialTopic&id=$1 last; rewrite "^/kds(/\w+|)(/|)$" /moviecore/server/mversionV1/index.php?ctl=mKds&act=index&type=$1 last; rewrite "^/kds/(\w+)/(\d{1,6}).html$" /moviecore/server/mversionV1/index.php?ctl=mKds&act=index&type=$1&subId=$2 last; rewrite "^/ivy/iframe_ivy_(\d{1,4})_(\d{1,4}).html$" /moviecore/server/mversion/index.php?ctl=iframeIvy&pageId=$1&posId=$2 last; } location ~ \.php { try_files $uri = /moviecore/404show.php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } error_page 404 = /moviecore/404show.php; }
3、nginx中,禁止单个目录(百度、搜狗、360、神马)蜘蛛的抓取。
server { listen 80; server_name v.km.com; access_log /opt/logs/tengine/v.km.com.access.log main; error_log /opt/logs/tengine/v.km.com.error.log warn; root /opt/case/ys.km.com; index index.php index.html index.htm; location /qiye { if ($http_user_agent ~* "Baiduspider|360Spider|Sogou spider|Sogou web spider|Yisouspider") { return 403; } proxy_pass http://b2b.88360.com/site-kmw/; proxy_set_header Host $proxy_host; proxy_set_header Connection close; } location / { rewrite "^/tv/([^-]{0,}-[^-]{0,}-[^-]{0,}-\d{0,}-?[^-]{0,}-?\d{0,}\.html)$" /moviecore/server/list/index.php?ctl=tvList&args=$1 last; } location ~ \.php { try_files $uri = /moviecore/404show.php; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } error_page 404 = /moviecore/404show.php; }
4、nginx rewrite 从一个文件重定向到另一个文件
server { listen 172.16.100.60:80; server_name download.xxx.com; access_log /opt/logs/tengine/download.xxx.com.access.log main; error_log /opt/logs/tengine/download.xxx.com.error.log warn; root /opt/case/download.xxx.com; index index.php index.html index.htm; location ~* ^/v2.0/([0-9a-zA-Z-_]+)/([0-9a-zA-Z-_]+)\-(((0|2)?\-)?[0-9]*)\.apk { try_files $uri @packapk; expires 7d; } location = /wtzw/free/test1.apk { rewrite ^ http://download.xxx.com/v2.0/qtest2.apk ; } location @packapk { proxy_pass http://127.0.0.1:8181; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~* \.apk { expires 7d; } location ~* \.(zip|jpg|png|gif|jpeg) { expires 365d; } }
5、nginx rewrite url 包含某个特殊字符的 跳转到另外一个
server { listen 443; server_name tj-adc.xxx.com; ssl on; ssl_certificate /opt/app/tengine/conf/ssl/xxx.com.pem; ssl_certificate_key /opt/app/tengine/conf/ssl/xxx.com.key; access_log /opt/logs/tengine_ssl/tj-adc.xxx.com.access.log ssl; error_log /opt/logs/tengine_ssl/tj-adc.xxx.com.error.log warn; root /opt/case/tj-adc.xxx.com; index index.php index.html index.htm; location /click { proxy_pass http://member/click; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /check { proxy_pass http://member/check; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location = /api/ksapiStat.php { proxy_pass http://kuaishou/click/kuaishou; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ \.php { rewrite ^/api/click.php?(.*) /click?$1 last; rewrite ^/api/(.*)Stat.php?(.*) /click?m=$1$2 last; rewrite ^/api/check.php?(.*) /check?$1 last; fastcgi_pass 127.0.0.1:9002; fastcgi_index index.php; include fastcgi.conf; } }
6、nginx rewrite 如果文件不存在,跳转到另外一个域名
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|thumb) { root /data/wwwroot/bbs.xxx.com; expires 10d; if (!-e $request_filename) { rewrite ^/data/attachment/forum/(.*)$ http://img.xxx.com/forum/$1 permanent; } }
7、nginx request_uri 配置只允许访问某些url
需求:只允许访问某些指定的url,否则返回指定404页面。 server { listen 443; server_name m.xxx.com; access_log /opt/logs/tengine_ssl/m.xxx.com.access.log ssl; error_log /opt/logs/tengine_ssl/m.xxx.com.error.log warn; root /opt/case/xxx.com/frontend/web; index index.php; #if ( $host !~ www ) { # rewrite ^.*$ https://m.xxx.com last; #} ssl on; ssl_certificate /opt/app/tengine/conf/ssl/xxx.com.pem; ssl_certificate_key /opt/app/tengine/conf/ssl/xxx.com.key; client_max_body_size 100M; location / { if (!-e $request_filename){ rewrite ^/(.*) /index.php?r=$1 last; } } location ~ \.php$ { if ($request_uri !~* "adapt-reader/index/|api/adapt-reader/chapters/|adapt-reader/404/|MP_verify_EeX9tzGqgqUDz42v.txt") { return https://m.xxx.com/adapt-reader/404/; } fastcgi_pass 127.0.0.1:9003; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } 需求: 某些特定的URL不做 http 跳转到 https,其他的都做https跳转 server { listen 80; server_name m.xxx.com; access_log /opt/logs/tengine/m.xxx.com.access.log main; error_log /opt/logs/tengine/m.xxx.com.error.log warn; root /opt/case/xxx.com/frontend/web; index index.php; #rewrite "(.*)" https://m.xxx.com/$1 permanent; location / { if (!-e $request_filename){ rewrite ^/(.*) /index.php?r=$1 last; } } location ~ \.php$ { ###实现代码 if ($request_uri !~* "MP_verify_EeX9tzGqgqUDz42v.txt") { rewrite "(.*)" https://m.xxx.com permanent; } fastcgi_pass 127.0.0.1:9003; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }