bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Nginx服務——rewrite模塊應用實戰-創新互聯

Nginx服務——rewrite模塊應用實戰

Demo 1:基于域名的跳轉

? 應用場景: 原域名即將不可用,現用新的域名代替

創新互聯主營吳橋網站建設的網絡公司,主營網站建設方案,APP應用開發,吳橋h5小程序定制開發搭建,吳橋網站營銷推廣歡迎吳橋等地區企業咨詢

? 理論結果: 輸入舊域名,自動跳轉到新域名,且其它參數不變

DNS方向

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm       //yum庫升級
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.zvmFF2: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y   #安裝nginx服務
[root@localhost ~]# yum install bind -y
[root@localhost ~]# vim /etc/named.conf 
###按照下面進行修改
options {
        listen-on port 53 { any; };           #監聽所有的53端口
        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; };            #允許所有通過

[root@localhost ~]# vim /etc/named.rfc1912.zones
####添加如下
zone "test.com" IN {
        type master;
        file "named.test";
};

[root@localhost ~]# cp -p /var/named/named.localhost /var/named/named.test
[root@localhost ~]# vim /var/named/named.test
#####按照下面進行修改
$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       192.168.142.128

[root@localhost ~]# systemctl start named
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

Nginx方向

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
####按下面進行更改
server {
    listen       80;
    server_name www.test.com;          #指定域名

    charset utf-8;                      #指定字符集
    access_log  /var/log/nginx/test.com-access.log  main;     #指定access日志文件位置

[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -atnp | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4028/nginx: master

此時,能夠對原域名進行正常的訪問

Nginx服務——rewrite模塊應用實戰

為了滿足實驗要求,現對新域名添加dns區域

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
location / {
####在location后面添加下面兩行
        if ($host = 'www.test.com') {
            rewrite ^/(.*)$ http://www.yun.com/$1 permanent;
        }

[root@localhost ~]# vim /etc/named.rfc1912.zones
###為新域名增添新的dns區域
zone "yun.com" IN {
        type master;
        file "named.yun";
};

[root@localhost ~]# cp -p /var/named/named.test /var/named/named.yun
[root@localhost ~]# systemctl restart named
[root@localhost ~]# systemctl restart nginx

實驗成功,成功從舊域名自動跳轉到新域名

Nginx服務——rewrite模塊應用實戰

Demo 2:基于客戶端IP跳轉

? 應用場景: 網站維護時僅有個別用戶能夠正常進行訪問,其余用戶僅能訪問維護頁面

? 理論結果: 一臺客戶機進行訪問時自動進行IP比對,按表進行分類

DNS方向

? 同Demo 1,不用進行改變。

Nginx方向

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
####按下面進行添加
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

   set $ip true;        #設定變量為true
   if ($remote_addr = "192.168.142.129") {           #匹配IP地址為“192.168.142.129”時
       set $ip false;                      #變量變更為false
   }
   if ($ip = true) {              #匹配變量為true時
       rewrite ^/(.*)$ /weihu.html;      #跳轉網頁到維護頁面
   }
   location = /weihu.html {       #匹配到維護頁面時
       root /usr/share/nginx/html;      #指定網頁站點
   }

[root@localhost ~]# systemctl restart nginx

此時,根據不同的ip地址,將會瀏覽到不同的頁面(正常頁面/維護頁面)
Nginx服務——rewrite模塊應用實戰

Nginx服務——rewrite模塊應用實戰

Demo 3:基于舊、新域名跳轉并添加目錄

? 應用場景: 將域名http://bbs.test.com下面的發帖都跳轉到 http://www.test.com/bbs,且域名跳轉后保持參數 不變

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
###按照下面進行修改,在location段下面添加
location /new {
#     if ($request_uri ~* ^/new) {
        rewrite /?(.*) http://www.test.com/bbs/$1 permanent;
}

[root@localhost ~]# systemctl restart nginx

Nginx服務——rewrite模塊應用實戰

Demo 4:基于參數匹配跳轉到指定頁面

? 應用場景: 用戶在輸入域名后,誤輸入了全為數字的錯誤頁面,應用后將自動跳轉回指定頁面

? 理論結果: 基于正則表達式的選擇

DNS方向

? 同上面所有,無需進行更改

Nginx方向

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
###按照下面進行修改,在location段下面添加
if ($request_uri ~* ^/(\d*).html$) {      #匹配以所有數字結尾的html文件
        rewrite (.*) http://www.test.com permanent;            #匹配零字或多字跳轉到網站主頁
    }

[root@localhost ~]# systemctl restart nginx

此時,在域名后添加一段數字組成的html網頁將自動跳轉回主頁。例:http://www.test.com/123456.htmlhttp://www.test.com/

Nginx服務——rewrite模塊應用實戰

Demo 5:基于PHP文件、具體頁面跳轉回首頁

基于PHP文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
###按照下面進行修改
##location段后添加
location ~* /upload/(.+)\.php$ {         #基于全部php文件
        rewrite (.*) http://www.test.com permanent;
    }

[root@localhost ~]# systemctl restart nginx

沒啥可說的,以php的文件均會自動跳轉回首頁

Nginx服務——rewrite模塊應用實戰
基于具體html頁面

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
###按照下面進行修改
##location段后添加
    location ~* /test.html$ {           #基于具體的html網頁
        rewrite (.*) http://www.test.com permanent;
    }

[root@localhost ~]# systemctl restart nginx

同上,訪問某個具體網頁文件就會跳轉回首頁

Nginx服務——rewrite模塊應用實戰

基于任意的html頁面

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
###按照下面進行修改
##location /后面添加
    if ($request_uri ~* ^/new/(.+)\.html$) {          #匹配到任意html頁面
        rewrite (.*) http://www.test.com permanent;      #跳轉到首頁
     }

[root@localhost ~]# systemctl restart nginx

此時,訪問任意的html格式的網頁將自動跳轉到首頁

Nginx服務——rewrite模塊應用實戰

感謝閱讀!!

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文標題:Nginx服務——rewrite模塊應用實戰-創新互聯
鏈接地址:http://vcdvsql.cn/article42/hdohc.html

成都網站建設公司_創新互聯,為您提供搜索引擎優化做網站品牌網站制作外貿網站建設品牌網站設計品牌網站建設

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

搜索引擎優化