濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > nginx里的rewrite跳轉(zhuǎn)的實(shí)現(xiàn)

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

熱門標(biāo)簽:百度地圖底圖標(biāo)注 電銷智能機(jī)器人試用 智能電銷機(jī)器人真的有用么 高德地圖標(biāo)注足跡怎么打標(biāo) 撫州市城區(qū)地圖標(biāo)注 新鄉(xiāng)牧野400電話申請(qǐng) 激光標(biāo)記地圖標(biāo)注 企業(yè)辦理400電話收費(fèi)標(biāo)準(zhǔn) 中國(guó)地圖標(biāo)注上各個(gè)省

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

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

有業(yè)務(wù)需求要變更,需要使用新域名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

瀏覽器測(cè)試

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

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

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

修改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';                                      //識(shí)別中文字符
  access_log /var/log/nginx/peihua.com-access.log main;
  #設(shè)置是否合法的IP標(biāo)志
  set $rewrite ture;
  #判斷是否為合法IP
  if ($remote_addr = "12.0.0.100") {
 set $rewrite false;
 }
 #非法IP進(jìn)行判斷打上標(biāo)記
 if ($rewrite = ture) {
 rewrite (.+) /maintenance.html;
 }
 #匹配標(biāo)記進(jìn)行跳轉(zhuǎn)站點(diǎn)
 location = /maintenance.html {
 root /usr/share/nginx/html;
 }

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

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

瀏覽器測(cè)試

使用ip地址為12.0.0.100 進(jìn)行訪問(wèn)http://www.peihua.com,可以正常訪問(wèn)
使用IP地址為12.0.0.99進(jìn)行訪問(wèn)http://www.peihua.com,顯示維護(hù)頁(yè)面

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

作用場(chǎng)景:基于舊域名跳轉(zhuǎn)到新域名后面加目錄,例如現(xiàn)在訪問(wèn)的是http://bbs.peihua.com.
現(xiàn)在需要將這個(gè)域名下面的發(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 需要更改主機(jī)域名解析,把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

瀏覽器訪問(wèn)

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

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

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

修改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匹配一個(gè)數(shù)字,0~9之間

修改會(huì)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

瀏覽器測(cè)試

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

五. 基于所有以php結(jié)尾及基于某一個(gè)頁(yè)面的跳轉(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

瀏覽器訪問(wèn)

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

六. 基于具體的某一個(gè)頁(yè)面進(jìn)行跳轉(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

瀏覽器訪問(wèn)

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

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

標(biāo)簽:臨汾 西安 南通 海西 辛集 延安 邯鄲 忻州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《nginx里的rewrite跳轉(zhuǎn)的實(shí)現(xiàn)》,本文關(guān)鍵詞  nginx,里的,rewrite,跳轉(zhuǎn),的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《nginx里的rewrite跳轉(zhuǎn)的實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于nginx里的rewrite跳轉(zhuǎn)的實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    扎兰屯市| 望城县| 岳阳县| 永宁县| 周口市| 调兵山市| 十堰市| 正定县| 德令哈市| 阳曲县| 沿河| 台州市| 溧水县| 龙井市| 喀喇沁旗| 宁陵县| 林周县| 浦北县| 思南县| 丰原市| 社旗县| 靖边县| 惠东县| 穆棱市| 外汇| 邵阳县| 沙洋县| 华池县| 农安县| 百色市| 武穴市| 潜山县| 巴南区| 彩票| 英吉沙县| 瑞安市| 阿克苏市| 丹凤县| 浦城县| 章丘市| 泌阳县|