1. 程式人生 > >常用nginx rewrite重定向-跳轉例項

常用nginx rewrite重定向-跳轉例項

常用nginx rewrite重定向-跳轉例項:

1,將www.myweb.com/connect 跳轉到connect.myweb.com

rewrite ^/connect$ http://connect.myweb.com permanent;

rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;

2,將connect.myweb.com 301跳轉到www.myweb.com/connect/ 

if ($host = "connect.myweb.com"){

rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;

    }

3,myweb.com 跳轉到www.myweb.com

if ($host != 'www.myweb.com' ) { 

rewrite ^/(.*)$ http://www.myweb.com/$1 permanent; 

    }

 

4,www.myweb.com/category/123.html 跳轉為 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

5,www.myweb.com/admin/ 下跳轉為www.myweb.com/admin/index.php?s=

if (!-e $request_filename){

rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;

    }

6,在後面新增/index.php?s=

if (!-e $request_filename){

    rewrite ^/(.*)$ /index.php?s=/$1 last;

    }

7,www.myweb.com/xinwen/123.html  等xinwen下面數字+html的連結跳轉為404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

8,http://www.myweb.com/news/radaier.html 301跳轉 http://www.myweb.com/strategy/

rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;

9,重定向 連結為404頁面

rewrite http://www.myweb.com/123/456.php /404.html last;

10, 禁止htaccess

location ~//.ht {

         deny all;

     }

11, 可以禁止/data/下多級目錄下.log.txt等請求;

location ~ ^/data {

     deny all;

     }

12, 禁止單個檔案

location ~ /www/log/123.log {

      deny all;

     }

13, http://www.myweb.com/news/activies/2014-08-26/123.html 跳轉為 http://www.myweb.com/news/activies/123.html

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;

14,nginx多條件重定向rewrite

如果需要開啟帶有play的連結就跳轉到play,不過/admin/play這個不能跳轉

        if ($request_filename ~ (.*)/play){ set $payvar '1';}
        if ($request_filename ~ (.*)/admin){ set $payvar '0';}
        if ($payvar ~ '1'){
                rewrite ^/ http://play.myweb.com/ break;
        }


15,http://www.myweb.com/?gid=6 跳轉為http://www.myweb.com/123.html

 if ($request_uri ~ "/\?gid\=6"){return  http://www.myweb.com/123.html;}

正則表示式匹配,其中:

* ~ 為區分大小寫匹配

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

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

檔案及目錄匹配,其中:

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

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

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

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

flag標記有:

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

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

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

* permanent 返回301永久重定向 位址列會顯示跳轉後的地址

nginx rewrite 301跳轉
jin6268890人評論157人閱讀2018-05-31 10:22:44
server {
listen 80;
server_name www.sss.com;
rewrite ^/(.*) http://home.sss.com/$1 permanent

Nginx網站常見的跳轉配置例項
2012hjtwyf關注0人評論20481人閱讀2017-05-03 10:44:36


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

下面是我工作中遇到的一些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

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;

}

我們經常訪問某個網站如果設定使 mgcrazy.com域名在使用者訪問的時候自動跳轉到 www.mgcrazy.com呢?在網上找了好多資料都沒有一個完整能解決的!以下是我的解決辦法!供大家學習和參考!

首先一、得在你的域名管理裡面定義 mgcrazy.com和www.mgcrazy.com指向你的主機ip地址,我們可以使用nslookup命令測試:直接輸入 nslookup mgcrazy.com和nslookup www.mgcrazy.com 都有指向ip的A記錄即可。
第二、我們才能在nginx裡面配置rewrite規則。開啟 nginx.conf檔案,找到你的server配置段:
server 

listen 80; 
server_name www.mgcrazy.com mgcrazy.com; 
if ($host != 'www.mgcrazy.com' ) { 
rewrite ^/(.*)$ http://www.mgcrazy.com/$1 permanent; 

 這樣就是使用者直接訪問mgcrazy.com直接跳轉的www.mgcrazy.com。即讓不帶www的域名跳轉到帶www的域名。
三、我們可以是多個二級域名、三級域名都可以隨意跳轉、或者讓它們都跳轉到chinaapp.sinaapp.com這個域名,新增如下語句即可:
server 

listen 80; 
server_name blog.mgcrazy.com wgkgood.gicp.net; 
if ($host = 'wgkgood.gicp.net' ) { 
rewrite ^/(.*)$ http://chinaapp.sinaapp.com/$1 permanent; 

 讓另外一個免費的二級域名wgkgood.gicp.net跳轉到我的部落格 http://chinaapp.sinaapp.com ;
這樣不至於放棄二級域名後,對搜尋引擎造成影響。對seo很有幫助。
更多nginx規則,歡迎大家一起學習!相互提高!我始終相信一個人的價值不在於自己取得了什麼,而在於自己給別人奉獻了什麼!