最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx rewrite跳轉(zhuǎn)應用場景詳解

 更新時間:2019年11月19日 11:43:05   作者:wx5d3a7feeb53cc  
這篇文章主要介紹了Nginx rewrite跳轉(zhuǎn)應用場景詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

應用場景1——基于域名的跳轉(zhuǎn)

公司舊域名www.accp.com ,因業(yè)務(wù)需求有變更,需要使用新域名www.kgc.com 代替
不能廢除舊域名
從舊域名跳轉(zhuǎn)到新域名,且保持其參數(shù)不變

實驗環(huán)境

Linux服務(wù)器(192.168.13.144)
測試機win7

1,安裝Nginx服務(wù)

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##安裝nginx官方源
警告:/var/tmp/rpm-tmp.vS0k20: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準備中...             ################################# [100%]
正在升級/安裝...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y  ##yum安裝nginx

2,修改nginx默認配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf ##修改默認配置文件
server {
    listen    80;
    server_name www.accp.com;  ##修改主機名

    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;  ##開啟日志服務(wù)

3,安裝bind解析服務(wù)

[root@localhost ~]# yum install bind -y

4,修改主配置文件(named.conf)

[root@localhost ~]# vim /etc/named.conf 
options {
                listen-on port 53 { any; };     ##監(jiān)聽所有
                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; };      ##允許所有

5,修改區(qū)域配置文件(named.rfc1912.zones)

[root@localhost ~]# vim /etc/named.rfc1912.zones  ##配置區(qū)域配置文件

zone "accp.com" IN {
                type master;
                file "accp.com.zone";       ##accp區(qū)域數(shù)據(jù)配置文件
                allow-update { none; };
};

6,修改區(qū)域數(shù)據(jù)配置文件(accp.com.zone)

[root@localhost ~]# cd /var/named/ 
[root@localhost named]# cp -p named.localhost accp.com.zone  ##復制模板
[root@localhost named]# vim accp.com.zone  ##修改區(qū)域配置文件

$TTL 1D
@    IN SOA @ rname.invalid. (
                                    1D   ; refresh
                                    1H   ; retry
                                    1W   ; expire
                                    3H )  ; minimum
                NS   @
                A    127.0.0.1
www IN A    192.168.13.144   ##本機地址
[root@localhost named]# systemctl start named   ##開啟dns服務(wù)
[root@localhost named]# systemctl stop firewalld.service  ##關(guān)閉防火墻
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx  ##開啟nginx服務(wù)
[root@localhost named]# netstat -ntap | grep nginx ##查看端口
tcp  0  0 0.0.0.0:80   0.0.0.0:*    LISTEN  4093/nginx: master 

7,用測試機測試網(wǎng)頁


8,修改配置文件,設(shè)置域名跳轉(zhuǎn)

[root@localhost named]# vim /etc/nginx/conf.d/default.conf ##修改配置文件
server {
    listen    80;
    server_name www.accp.com;

    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;

    location / {
        if ($host = "www.accp.com"){    ##匹配如果域名是老域名
                rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;  ##則永久設(shè)置跳轉(zhuǎn)新域名
        }
        root  /usr/share/nginx/html;
        index index.html index.htm;
    }

9,添加新域名解析

[root@localhost named]# vim /etc/named.rfc1912.zones 

zone "kgc.com" IN {
                type master;
                file "kgc.com.zone";       ##accp區(qū)域數(shù)據(jù)配置文件
                allow-update { none; };
};

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##復制區(qū)域數(shù)據(jù)配置文件為kgc的數(shù)據(jù)配置文件
[root@localhost named]# systemctl restart named  ##重啟解析服務(wù)
[root@localhost named]# systemctl restart nginx   ##重啟nginx服務(wù)

10,用老域名訪問,查看跳轉(zhuǎn)


11,老域名后加上參數(shù),查看跳轉(zhuǎn)新域名時是否有參數(shù)


應用場景2——基于客戶端IP訪問跳轉(zhuǎn)

公司業(yè)務(wù)版本上線,所有IP訪問任何內(nèi)容都顯示一個固定維護頁面,只有公司IP訪問正常

1,修改Nginx默認配置文件

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

server {
    listen    80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    #設(shè)置是否合法的IP標志
    set $rewrite true;     ##設(shè)置變量為真
    #判斷是否為合法的IP
    if ($remote_addr = "192.168.13.140"){
        set $rewrite false;  ##匹配合法IP,將變量設(shè)置為假,正常跳轉(zhuǎn)頁面
    }
    #非法IP進行判斷打上標記
    if ($rewrite = true){        ##匹配非法IP,跳轉(zhuǎn)到main的網(wǎng)頁
        rewrite (.+) /main.html;
    }
    #匹配標記進行跳轉(zhuǎn)站點
    location = /main.html {       ##精確匹配
        root /usr/share/nginx/html;  ##站點路徑
    }

    location / {
        root  /usr/share/nginx/html;
        index index.html index.htm;
    } 

2,創(chuàng)建非法IP站點及main的網(wǎng)頁頁面

[root@localhost conf.d]# cd /usr/share/nginx/html/ ##切換到站點中
[root@localhost html]# vim main.html  ##編輯非法IP訪問網(wǎng)頁內(nèi)容
<h1>this is test web</h1>
[root@localhost html]# systemctl restart nginx  ##重啟Nginx服務(wù)

3,訪問網(wǎng)頁


應用場景3——基于舊,新域名跳轉(zhuǎn)并加目錄

將域名http://bbs.accp.com 下面的發(fā)帖都跳轉(zhuǎn)到http://www.accp.com/bbs 且域名跳轉(zhuǎn)后保持參數(shù)不變

1,修改Nginx默認配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf  ##修改默認配置文件
server {
    listen    80;
    server_name bbs.accp.com;  ##修改服務(wù)名稱

    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    location /post {     ##用location匹配post目錄
        rewrite (.+) http://www.accp.com/bbs$1 permanent;  ##永久重定向跳轉(zhuǎn)
    }

2,修改dns的區(qū)域數(shù)據(jù)配置文件(accp.com.zone)

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.com.zone  ##修改區(qū)域數(shù)據(jù)配置文件
$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    192.168.13.144
[root@localhost named]# systemctl restart named  ##重啟解析服務(wù)
[root@localhost named]# systemctl restart nginx   ##重啟Nginx服務(wù)
[root@localhost named]# echo "nameserver 192.168.13.144" > /etc/resolv.conf 
##將解析服務(wù)器地址放到本地解析配置文件中

3,測試網(wǎng)頁


應用場景4——基于參數(shù)匹配的跳轉(zhuǎn)

瀏覽器訪問http://www.accp.com/100-(100|200)-100.html 跳轉(zhuǎn)到http://www.accp.com 頁面

1,修改Nginx默認配置文件

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

server {
    listen    80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$){    
    ##匹配正則開頭為100-(100|200)-一次多次的整數(shù)html為結(jié)尾的
        rewrite (.*) http://www.accp.com permanent;    ##永久重定向跳轉(zhuǎn)到主頁
    }

2,修改dns區(qū)域數(shù)據(jù)配置文件

[root@localhost conf.d]# vim /var/named/accp.com.zone ##修改區(qū)域數(shù)據(jù)配置文件
www IN A    192.168.13.144  
[root@localhost conf.d]# systemctl restart named ##重啟解析服務(wù) 
[root@localhost conf.d]# systemctl restart nginx   ##重啟Nginx服務(wù)

3,測試網(wǎng)頁


應用場景5——基于目錄下所有PHP文件跳轉(zhuǎn)

訪問http://www.accp.com/upload/1.php 跳轉(zhuǎn)到首頁

1,修改Nginx默認配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf  ##修改默認配置文件
server {
    listen    80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    location ~* /upload/.*\.php$ {     ##匹配不分大小寫,匹配upload后零次或多次以.php為結(jié)尾的
        rewrite (.+) http://www.accp.com permanent;  ##跳轉(zhuǎn)到首頁
    }
[root@localhost conf.d]# systemctl restart nginx  ##重啟Nginx服務(wù)

2,測試網(wǎng)頁


應用場景6——基于最普通url請求的跳轉(zhuǎn)

訪問一個具體的頁面跳轉(zhuǎn)到首頁

1,修改Nginx默認配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf  ##修改Nginx默認配置文件
server {
    listen    80;
    server_name www.accp.com;
    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;
    location ~* ^/abc/123.html {    ##匹配某一個特定的網(wǎng)頁
        rewrite (.+) http://www.accp.com permanent; ##跳轉(zhuǎn)到首頁
    }
[root@localhost conf.d]# systemctl restart nginx  ##重啟Nginx服務(wù)

2,測試網(wǎng)頁


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Linux環(huán)境下nginx搭建簡易圖片服務(wù)器

    Linux環(huán)境下nginx搭建簡易圖片服務(wù)器

    這篇文章主要介紹了Linux環(huán)境下nginx搭建簡易圖片服務(wù)器,需要的朋友可以參考下
    2014-10-10
  • Nginx配置之location的匹配優(yōu)先級淺析

    Nginx配置之location的匹配優(yōu)先級淺析

    這篇文章主要給大家介紹了關(guān)于Nginx配置之location的匹配優(yōu)先級的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-11-11
  • Nginx配置使用詳解

    Nginx配置使用詳解

    Nginx是一個高性能的HTTP和反向代理web服務(wù)器。本文詳細講解了Nginx配置使用的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • 為Nginx添加mp4流媒體支持

    為Nginx添加mp4流媒體支持

    這篇文章主要介紹了為Nginx添加mp4流媒體支持的的相關(guān)資料,需要的朋友可以參考下
    2014-12-12
  • Nginx安裝后常用功能配置基礎(chǔ)篇

    Nginx安裝后常用功能配置基礎(chǔ)篇

    這篇文章主要介紹了Nginx安裝后常用的功能配置,為了在使用中更高效簡潔,Nginx安裝后通常會進行一些常用的配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-03-03
  • Nginx Location 指令簡明指南

    Nginx Location 指令簡明指南

    這篇文章主要介紹了Nginx Location 指令簡明指南,本文講解了它的基本語法、匹配過程、配置實例和全局變量,需要的朋友可以參考下
    2015-04-04
  • nginx 配置特定IP訪問的實現(xiàn)

    nginx 配置特定IP訪問的實現(xiàn)

    本文主要介紹了nginx 配置特定IP訪問的實現(xiàn),通過合理設(shè)置Nginx的配置文件,管理員可以根據(jù)實際需求,靈活地控制對網(wǎng)站資源的訪問,提高網(wǎng)站的安全性,感興趣的可以了解一下
    2024-01-01
  • Nginx 負載均衡是什么以及該如何配置

    Nginx 負載均衡是什么以及該如何配置

    這篇文章主要介紹了Nginx 負載均衡是什么以及該如何配置,幫助大家更好的理解和使用Nginx服務(wù)器,感興趣的朋友可以了解下
    2021-01-01
  • 通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源

    通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源

    這篇文章為大家詳細主要介紹了如何通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源,文中通過圖文進行了詳細的講解,有需要的小伙伴可以了解下
    2023-10-10
  • nginx訪問控制的實現(xiàn)示例

    nginx訪問控制的實現(xiàn)示例

    這篇文章主要介紹了nginx訪問控制的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11

最新評論

乐昌市| 宝坻区| 广宁县| 钟祥市| 丰镇市| 会东县| 宁明县| 古浪县| 奇台县| 涡阳县| 贺兰县| 大邑县| 宝鸡市| 永泰县| 监利县| 突泉县| 梓潼县| 彩票| 南康市| 莲花县| 门源| 遵化市| 乌兰浩特市| 邵阳县| 桑日县| 娱乐| 齐齐哈尔市| 兴化市| 嘉祥县| 银川市| 西吉县| 富宁县| 龙游县| 嘉荫县| 青海省| 襄樊市| 青田县| 绥芬河市| 阿坝县| 临泽县| 舞阳县|