下面是我工作中遇到的一些rewrite示例。提供給大家分享
正規表達式比對,其中:
-
- ~ 為區分大小寫比對
-
- ~* 為不區分大小寫比對
-
- !和!*分别為區分大小寫不比對及不區分大小寫不比對
檔案及目錄比對,其中:
-
- -f和!-f用來判斷是否存在檔案
-
- -d和!-d用來判斷是否存在目錄
-
- -e和!-e用來判斷是否存在檔案或目錄
-
- -x和!-x用來判斷檔案是否可執行
flag标記有:
-
- last 相當于Apache裡的[L]标記,表示完成rewrite
-
- break 終止比對, 不再比對後面的規則
-
- redirect 傳回302臨時重定向 位址欄會顯示跳轉後的位址
-
- permanent 傳回301永久重定向 位址欄會顯示跳轉後的位址
1、換域名後導流到新域名
需要将之前用的
www.a.com域名的流量全部跳轉到
www.b.com實作效果:比如通路
www.a.com/123.html自動跳到
www.b.com/123.html我們用nginx實作
cd /etc/nginx/sites-available
vi mysite
增加rewrite指令
server {
listen 80;
server_name www.a.com;
rewrite ^/(.*)$ http://www.b.com/$1 permanent;
}
2、主域名跳轉到www域名
比如将主域名
xxx.com跳轉到
www.xxx.com主域名跳轉到www域名
listen 80;
server_name xxx.com;
rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
listen 80;
server_name www.xxx.com;
#已省略餘下通用配置内容
3、主目錄跳轉,子目錄不跳轉
a.com和
都跳到
www.a.com/123不跳listen 80;
server_name www.a.us a.us;
#根目錄跳轉
location / {
rewrite .+ http://www.b.com/ permanent;
}
#非根目錄本地執行
location ~* /.+ {
#已省略餘下通用配置内容
}
4、301跳轉設定:
listen 80;
server_name 123.com;
rewrite ^/(.*)
http://456.com/$1permanent;
access_log off;
permanent – 傳回永久重定向的HTTP狀态301
5、302跳轉設定:
listen 80;
server_name 123.com;
redirect;
redirect – 傳回臨時重定向的HTTP狀态302
6、禁止htaccess
location ~//.ht {
deny all;
7、禁止多個目錄
location ~ ^/(cron|templates)/ {
deny all;
break;
8、禁止以/data開頭的檔案
可以禁止/data/下多級目錄下.log.txt等請求;
location ~ ^/data {
deny all;
9、禁止單個目錄
不能禁止.log.txt能請求
location /searchword/cron/ {
deny all;
10、禁止單個檔案
location ~ /data/sql/data.sql {
deny all;
11、給favicon.ico和robots.txt設定過期時間;
這裡為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;
12、設定某個檔案的過期時間;這裡為600秒,并不記錄通路日志
location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
13、檔案反盜鍊并設定過期時間
這裡的return 412 為自定義的http狀态碼,預設為403,友善找出正确的盜鍊的請求
“rewrite ^/
http://leech.c1gstudio.com/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 ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
break;
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
14、隻充許固定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;
15、将多級目錄下的檔案轉成一個檔案,增強seo效果
/job-123-456-789.html 指向/job/123/456/789.html
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)/.html
1/
3.html last;
16、将根目錄下某個檔案夾指向2級目錄
如/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那麼浏覽器位址欄顯是/location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)
1/$2 last;
上面例子有個問題是通路/shanghai 時将不會比對
rewrite ^/([0-9a-z]+)job
1/ last;
這樣/shanghai 也可以通路了,但頁面中的相對連結無法使用,
如./list_1.html真實位址是/area/shanghia/list_1.html會變成/list_1.html,導至無法通路
那我加上自動跳轉也是不行咯
(-d $request_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,是以沒有效果
if (-d $request_filename){
rewrite /(.*)([/])
host/
2/ permanent;
知道原因後就好辦了,讓我手動跳轉吧
1job/ permanent;
17、檔案和目錄不存在的時候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
18、域名跳轉
server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
19、多域名轉向
server_name
www.c1gstudio.com www.c1gstudio.net;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "c1gstudio/.net") {
rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
20、三級域名跳轉
if (
") {
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;