相信你們在平常運維工做中若是你用到nginx做爲前端反向代理服務器的話,你會對nginx的rewrite又愛又恨,愛它是由於你搞定了它,完成了開發人員的跳轉需求後你會以爲很爽,以爲真的很強大,恨它是由於當一些稀奇古怪跳轉的需求時候你會沒有頭緒、百般調試、上網求神拜佛都搞不定的時候真是想死的心都有了,固然網上也有不少nginx rewrite的經典示例,可是我感受對個人工做都沒有太大的幫助php
下面是我工做中遇到的一些rewrite示例。提供給你們分享css
正則表達式匹配,其中:html
~ 爲區分大小寫匹配前端
~* 爲不區分大小寫匹配nginx
文件及目錄匹配,其中:web
-f和!-f用來判斷是否存在文件正則表達式
-d和!-d用來判斷是否存在目錄sql
-e和!-e用來判斷是否存在文件或目錄瀏覽器
flag標記有:緩存
last 至關於Apache裏的[L]標記,表示完成rewrite
break 終止匹配, 再也不匹配後面的規則
redirect 返回302臨時重定向 地址欄會顯示跳轉後的地址
一、換域名後導流到新域名
須要將以前用的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;
}
二、主域名跳轉到www域名
好比將主域名xxx.com 跳轉到www.xxx.com
cd /etc/nginx/sites-available
vi mysite
#主域名跳轉到www域名
server {
listen 80; server_name xxx.com; rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
}
server {
listen 80; server_name www.xxx.com; #已省略餘下通用配置內容
}
三、主目錄跳轉,子目錄不跳轉
a.com和www.a.com都跳到www.b.com
www.a.com/123不跳
server {
listen 80; server_name www.a.us a.us; #根目錄跳轉 location / { rewrite .+ http://www.b.com/ permanent; } #非根目錄本地執行 location ~* /.+ { #已省略餘下通用配置內容 }
}
四、301跳轉設置:
server {
listen 80;
server_name 123.com;
rewrite ^/(.*) http://456.com/$1 permanent;
access_log off;
}
permanent – 返回永久重定向的HTTP狀態301
五、302跳轉設置:
server {
listen 80;
server_name 123.com;
rewrite ^/(.*) http://456.com/$1 redirect;
access_log off;
}
redirect – 返回臨時重定向的HTTP狀態302
六、禁止htaccess
location ~//.ht {
deny all;
}
七、禁止多個目錄
location ~ ^/(cron|templates)/ {
deny all; break;
}
八、禁止以/data開頭的文件
能夠禁止/data/下多級目錄下.log.txt等請求;
location ~ ^/data {
deny all;
}
九、禁止單個目錄
不能禁止.log.txt能請求
location /searchword/cron/ {
deny all;
}
十、禁止單個文件
location ~ /data/sql/data.sql {
deny all;
}
十一、給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;
}
十二、設定某個文件的過時時間;這裏爲600秒,並不記錄訪問日誌
location ^~ /html/scripts/loadhead_1.js {
access_log off; root /opt/lampp/htdocs/web; expires 600; break;
}
1三、文件反盜鏈並設置過時時間
這裏的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;
}
1四、只充許固定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;
1五、將多級目錄下的文件轉成一個文件,加強seo效果
/job-123-456-789.html 指向/job/123/456/789.html
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /job/$1/$2/jobshow_$3.html last;
1六、將根目錄下某個文件夾指向2級目錄
如/shanghaijob/ 指向 /area/shanghai/
若是你將last改爲permanent,那麼瀏覽器地址欄顯是/location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
上面例子有個問題是訪問/shanghai 時將不會匹配
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
這樣/shanghai 也能夠訪問了,但頁面中的相對連接沒法使用,
如./list_1.html真實地址是/area/shanghia/list_1.html會變成/list_1.html,導至沒法訪問
那我加上自動跳轉也是不行咯
(-d $request_filename)它有個條件是必需爲真實目錄,而個人rewrite不是的,因此沒有效果
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
知道緣由後就好辦了,讓我手動跳轉吧
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
1七、文件和目錄不存在的時候重定向:
if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}
1八、域名跳轉
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;
}
1九、多域名轉向
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 ($http_host ~ "^(.)/.i/.c1gstudio/.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1; break;
}