1. 程式人生 > >Nginx跳轉的配置例項

Nginx跳轉的配置例項

相信大家在日常運維工作中如果你用到nginx作為前端反向代理伺服器的話,你會對nginx的rewrite又愛又恨,愛它是因為你搞定了它,完成了開發人員的跳轉需求後你會覺得很爽,覺得真的很強大,恨它是因為當一些稀奇古怪跳轉的需求時候你會沒有頭緒、百般除錯、上網求神拜佛都搞不定的時候真是想死的心都有了,當然網上也有很多nginx rewrite的經典示例,但是我感覺對我的工作都沒有太大的幫助

下面是我工作中遇到的一些rewrite示例。提供給大家分享

正則表示式匹配,其中:

  1. * ~ 為區分大小寫匹配

  2. * ~* 為不區分大小寫匹配

  3. * !~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配

檔案及目錄匹配,其中:

  1. * -f和!-f用來判斷是否存在檔案

  2. * -d和!-d用來判斷是否存在目錄

  3. * -e和!-e用來判斷是否存在檔案或目錄

  4. * -x和!-x用來判斷檔案是否可執行

flag標記有:

  1. * last 相當於Apache裡的[L]標記,表示完成rewrite

  2. * break 終止匹配, 不再匹配後面的規則

  3. * redirect 返回302臨時重定向 位址列會顯示跳轉後的地址

  4. * 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

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;

    #已省略餘下通用配置內容

}

3、主目錄跳轉,子目錄不跳轉

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 ~* /.+ {

            #已省略餘下通用配置內容

        }

}

4、301跳轉設定:

server {
       listen                   80;
       server_name     123.com;
       rewrite ^/(.*) http://456.com/$1 permanent;
       access_log off;
}

permanent – 返回永久重定向的HTTP狀態301

5、302跳轉設定:

server {
       listen                    80;
       server_name      123.com;
       rewrite ^/(.*) http://456.com/$1 redirect;
       access_log off;
}

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$ /job/$1/$2/jobshow_$3.html last;

16、將根目錄下某個資料夾指向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;

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 ($http_host ~* "^(.*)/.i/.c1gstudio/.com$") {

    rewrite ^(.*) http://top.yingjiesheng.com$1;

    break;

}