rewrite與location Nginx重寫功能( 三 )

4.2.2 實例操作:基于客戶端 IP 訪問跳轉
(1)修改配置文件

rewrite與location Nginx重寫功能

文章插圖

rewrite與location Nginx重寫功能

文章插圖
(2)檢查配置文件并重啟服務
rewrite與location Nginx重寫功能

文章插圖
(3)創建跳轉后的網頁目錄和內容
rewrite與location Nginx重寫功能

文章插圖
(4)瀏覽器訪問測試
本機訪問:
rewrite與location Nginx重寫功能

文章插圖
其它主機訪問:
現在域名IP地址做映射:
rewrite與location Nginx重寫功能

文章插圖
然后再訪問:
rewrite與location Nginx重寫功能

文章插圖
4.3基于舊域名跳轉到新域名后面加目錄4.3.1基于舊域名跳轉到新域名后面加目錄的操作步驟
現在訪問的是 http://bbs.fzr.com/test,現在需要將這個域名下面的訪問都跳轉到http://www.fzr.com/bbs/test
vim /usr/local/nginx/conf/nginx.confserver {    listen       80;    server_name  bbs.fzr.com;        #域名修改    charset utf-8;    access_log  /var/log/nginx/bbs.fzr.com-access.log;    #添加    location /test {        rewrite (.+) http://www.fzr.com/bbs$1 permanent;     #這里的$1為位置變量 , 代表/test    }    location / {        root   html;        index  index.html index.htm;    }}mkdir -p /usr/local/nginx/html/bbs/postecho "this is web"  >> /usr/local/nginx/html/bbs/post/1.htmlecho "192.168.10.10 bbs.fzr.com www.fzr.com"  >> /etc/hostssystemctl restart nginx4.3.2實例操作:基于舊域名跳轉到新域名后面加目錄
(1)修改主配置文件
rewrite與location Nginx重寫功能

文章插圖

rewrite與location Nginx重寫功能

文章插圖
(2)重啟服務并創建網頁文件
rewrite與location Nginx重寫功能

文章插圖

rewrite與location Nginx重寫功能

文章插圖

rewrite與location Nginx重寫功能

文章插圖
(3)添加映射關系并使用瀏覽器訪問測試
rewrite與location Nginx重寫功能

文章插圖
本機測試:
rewrite與location Nginx重寫功能

文章插圖

rewrite與location Nginx重寫功能

文章插圖
4.4基于參數匹配的跳轉4.4.1 基于參數匹配的跳轉的步驟
訪問http://www.fzr.com/100-(100|200)-100.html 跳轉到http://www.fzr.com頁面
vim /usr/local/nginx/conf/nginx.confserver {    listen       80;    server_name  www.fzr.com;        #域名修改    charset utf-8;    access_log  /var/log/nginx/fzr.kgc.com-access.log;    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {        rewrite (.*) http://www.fzr.com permanent;    }    location / {        root   html;        index  index.html index.htm;    }systemctl restart nginx解釋:$request_uri: 包含請求參數的原始URI , 不包含主機名,如: http://www.fzr.com/abc/bbs/index.html?a=1&b=2中的/abc/bbs/index.php?a=1&b=2$uri:這個變量指當前的請求URI,不包括任何參數,如: /abc/bbs/index.html$document_uri: 與$uri相同,這個變量指當前的請求URI,不包括任何傳遞參數 , 如:/abc/bbs/index.html
用瀏覽器訪問 http://www.fzr.com/100-200-100.html 或 http://www.fzr.com/100-100-100.html 跳轉到http://www.fzr.com頁面

推薦閱讀