主頁 > 知識庫 > nginx里的rewrite跳轉(zhuǎn)的實現(xiàn)

nginx里的rewrite跳轉(zhuǎn)的實現(xiàn)

熱門標簽:百度競價點擊價格的計算公式 使用U盤裝系統(tǒng) 檢查注冊表項 網(wǎng)站建設 美圖手機 硅谷的囚徒呼叫中心 智能手機 阿里云

一. 新舊域名跳轉(zhuǎn)

作用場景:基于域名的跳轉(zhuǎn),現(xiàn)在公司舊域名:www.peihua.com

有業(yè)務需求要變更,需要使用新域名www.zhenguo.com代替,但是舊域名不能廢除。需要跳轉(zhuǎn)到新域名上,而且后面的參數(shù)保持不變

配置dns,分別配置www.peihua.com(old)和www.zhenguo.com(new)解析

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

(必須要有官方源才能yum安裝nginx)

yum install nginx -y
rpm -qc nginx    //查找配置文件

修改nginx的配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 

server {
  listen    80;
  server_name www.peihua.com;                            #域名修改

#charset koi8-r;
  access_log /var/log/nginx/peihua.com-access.log main;        #日志修改

location / {
    #域名重定向
    if ($host = 'www.peihua.com') {
 rewrite ^/(.*)$ http://www.zhenguo.com/$1 permanent;
    }
    root  /usr/share/nginx/html;
    index index.html index.htm;
  }

[root@localhost named]# systemctl restart nginx

配置域名解析

yum -y install bind

#修改主配置文件
[root@localhost conf.d]# vim /etc/named.conf
options {
    listen-on port 53 { any; };      //修改成any
    listen-on-v6 port 53 { ::1; };
    directory    "/var/named";
    dump-file    "/var/named/data/cache_dump.db";
    statistics-file "/var/named/data/named_stats.txt";
    memstatistics-file "/var/named/data/named_mem_stats.txt";
    recursing-file "/var/named/data/named.recursing";
    secroots-file  "/var/named/data/named.secroots";
    allow-query   { any; };       //修改成any

#修改區(qū)域配置文件
[root@localhost conf.d]# vim /etc/named.rfc1912.zones 
zone "peihua.com" IN {
    type master;
    file "peihua.com.zone";
    allow-update { none; };
};

zone "zhenguo.com" IN {
    type master;
    file "zhenguo.com.zone";
    allow-update { none; };
};

#修改區(qū)域數(shù)據(jù)文件
[root@localhost conf.d]# cd /var/named/
[root@localhost named]# cp -p named.localhost peihua.com.zone
[root@localhost named]# cp -p peihua.com.zone zhenguo.com.zone

[root@localhost named]# systemctl start named

瀏覽器測試

瀏覽器輸入模擬訪問 http://www.peihua.com/test/1/index.php(雖然這個請求內(nèi)容) 是不存在的)跳轉(zhuǎn)到
http://www.zhneguo.com/test/1/index.php,從headers 里面 可以看到301,實現(xiàn)了永久跳轉(zhuǎn),而且域名后的參數(shù)也正常跳轉(zhuǎn)。

二. 基于IP訪問跳轉(zhuǎn)

作用場景:基于客戶端IP訪問跳轉(zhuǎn),例如今天公司業(yè)務版本上線,所有IP 訪問任何內(nèi)容都顯示一個固定維護頁面,只有公司IP:12.0.0.100訪問正常

修改nginx配置文件

[root@localhost html]# vim /etc/nginx/conf.d/default.conf 

server {
  listen    80;
  server_name www.peihua.com;

 #charset koi8-r;
  charset 'utf-8';                                      //識別中文字符
  access_log /var/log/nginx/peihua.com-access.log main;
  #設置是否合法的IP標志
  set $rewrite ture;
  #判斷是否為合法IP
  if ($remote_addr = "12.0.0.100") {
 set $rewrite false;
 }
 #非法IP進行判斷打上標記
 if ($rewrite = ture) {
 rewrite (.+) /maintenance.html;
 }
 #匹配標記進行跳轉(zhuǎn)站點
 location = /maintenance.html {
 root /usr/share/nginx/html;
 }

給maintenance.html添加自定義頁面內(nèi)容

[root@localhost html]# cat /usr/share/nginx/html/maintenance.html 
<h1>網(wǎng)站正在維護</h1>

瀏覽器測試

使用ip地址為12.0.0.100 進行訪問http://www.peihua.com,可以正常訪問
使用IP地址為12.0.0.99進行訪問http://www.peihua.com,顯示維護頁面

三. 基于舊域名跳轉(zhuǎn)到新域名后面加目錄

作用場景:基于舊域名跳轉(zhuǎn)到新域名后面加目錄,例如現(xiàn)在訪問的是http://bbs.peihua.com.
現(xiàn)在需要將這個域名下面的發(fā)帖都跳轉(zhuǎn)到http://www.peihua.com/bbs,注意保持域名跳轉(zhuǎn)后 的參數(shù)不變

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

  listen    80;
  server_name bbs.peihua.com;
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  location /post {
       rewrite (.+) http://www.peihua.com/bbs$1 permanent;
  }

注意:accp.com.zone 需要更改主機域名解析,把www改成 bbs

[root@localhost named]# cd /var/named
[root@localhost named]# vim peihua.com.zone 
$TTL 1D
@    IN SOA @ rname.invalid. (
                    0    ; serial
                    1D   ; refresh
                    1H   ; retry
                    1W   ; expire
                    3H )  ; minimum
    NS   @
    A    127.0.0.1
bbs IN A    12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

瀏覽器訪問

瀏覽器訪問 http://bbs.peihua.com/post/a.html,被跳轉(zhuǎn)稱為www.peihua.com/bbs/post/a.txt

四. 基于參數(shù)匹配的跳轉(zhuǎn)

作用場景:基于參數(shù)匹配的跳轉(zhuǎn),例如現(xiàn)在訪問 http://www.peihua.com/100-(100 | 200)-100.html
跳轉(zhuǎn)到http://www.peihua.com頁面

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
  listen    80;
  server_name www.peihua.com;
  
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  
  if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
     rewrite (.*) http://www.peihua.com permanent;
  } 

注意:\d匹配一個數(shù)字,0~9之間

修改會dns,把bbs改成www

[root@localhost named]# vim peihua.com.zone 
$TTL 1D
@    IN SOA @ rname.invalid. (
                    0    ; serial
                    1D   ; refresh
                    1H   ; retry
                    1W   ; expire
                    3H )  ; minimum
    NS   @
    A    127.0.0.1
www IN A    12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

瀏覽器測試

瀏覽器訪問 http://www.peihua.com/100-200-26.html,被跳轉(zhuǎn)稱為 www.peihua.com首頁

五. 基于所有以php結(jié)尾及基于某一個頁面的跳轉(zhuǎn)

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
  listen    80;
  
  server_name www.peihua.com;
  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;
  location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.peihua.com permanent;
  }


[root@localhost named]# systemctl restart nginx

瀏覽器訪問

瀏覽器訪問 http://www.peihua.com/upload/a.php,被跳轉(zhuǎn)稱為 www.peihua.com

六. 基于具體的某一個頁面進行跳轉(zhuǎn)/abc/123.html

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
  listen    80;
  server_name www.peihua.com;

  #charset koi8-r;
  charset 'utf-8';
  access_log /var/log/nginx/peihua.com-access.log main;

  location ~* ^/abc/123.html {
    rewrite (.+) http://www.peihua.com permanent;
  }
  
[root@localhost named]# systemctl restart nginx

瀏覽器訪問

瀏覽器訪問 http://www.peihua.com/abc/123.html,被跳轉(zhuǎn)稱為 www.peihua.com

到此這篇關于nginx里的rewrite跳轉(zhuǎn)的實現(xiàn)的文章就介紹到這了,更多相關nginx rewrite跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

標簽:懷化 湖北 煙臺 黃山 通遼 山南 湘潭 賀州

巨人網(wǎng)絡通訊聲明:本文標題《nginx里的rewrite跳轉(zhuǎn)的實現(xiàn)》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266