目錄
- 一、Nginx Rewrite概述
- 1.1、 Nginx Rewrite概述
- 二、Nginx Rewrite基本操作
- 2.1、Rewrite命令
- 2.2、location分類和優先級
- 2.2.1、location分類
- 2.2.2、location優先級
- 2.2.3、location優先級規則
- 三、Rewrite使用場景實驗
- 搭建基礎環境
一、Nginx Rewrite概述
1.1、 Nginx Rewrite概述
概述:
現在Nginx已經成為很多公司作為前端反向代理服務器的首選,在實際作業中往往會
遇到很多跳轉(重寫URL)的需求,比如更換域名后需要保持舊的域名能跳轉到新的域名上、某網頁發生改變需要跳轉到新的頁面、網站防盜鏈等等需求,如果在后端使用的Apache服務器,雖然也能做跳轉,規則庫也很強大,但是用Nginx跳轉效率會更高,
-
跳轉場景
1、可以調整用戶瀏覽的URL,看起來更規范,合乎開發及產品人員的需求,
2、為了讓搜索引擎搜錄網站內容及用戶體驗更好,企業會將動態URL地址偽裝成靜態地址提供服務,
3、網址換新域名后,讓舊的訪問跳轉到新的域名上,例如,訪問京東的360buy.com會跳轉到jd.com,
4、根據特殊變數、目錄、客戶端的資訊進行URL調整等, -
跳轉實作
Nginx是通過ngx_http_rewrite_module模塊支持url重寫、支持if條件判斷,但不支持else,另外該模塊需要 PCRE支持,應在編譯Nginx時指定PCRE 支持,默認已經安裝,根據相關變數重定向和選擇不同的配置,從一個location跳轉到另一個location,不過這樣的回圈最多可以執行10次,超過后Nginx將回傳500錯誤,同時,重寫模塊包含set指令,來創建新的變數并設其值,這在有些情景下非常有用的,如記錄條件標識、傳遞引數到其他location、記錄做了什么等等,rewrite功能就是,使用Nginx提供的全域變數或自己設定的變數,結合正則運算式和標志位實作url重寫以及重定向,

- Rewrite實際場景
■Nginx跳轉需求的實作方式
①使用rewrite進行匹配跳轉
②使用if匹配全域變數后跳轉
③使用location匹配在跳轉
二、Nginx Rewrite基本操作
2.1、Rewrite命令
- Rewrite命令語法
rewrite < regex > < replacement > [flag]
regex:正則運算式
replacement :跳轉后的內容
flag:rewrite支持的flag標記 - flag標記說明
| 標記 | 說明 |
|---|---|
| last | 相當于Apache的【L】標記,表示完成rewrite |
| break | 本條規則匹配完成即終止,不在匹配后面的任何規則 |
| redirect | 回傳302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址,爬蟲不會更新url |
| permanent | 回傳301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址,爬蟲更新url |
- last 和 break 比較
| last | break | |
|---|---|---|
| 使用場景 | 一般寫在server和if中 | 一般使用在location中 |
| URL匹配 | 不終止重寫后的url匹配 | 終止重寫后的url匹配 |
2.2、location分類和優先級
2.2.1、location分類
- location= patt {} [精準匹配]
- location patt {} [一般匹配]
- location ~ patt {} [一般匹配]
| 標記 | 說明 |
|---|---|
| ~ | 執行一個正則匹配,區分大小寫 |
| ~* | 執行一個正則匹配,不區分大小寫 |
| !~ | 執行一個正則匹配,區分大小寫不匹配 |
| !~* | 執行一個正則匹配,不區分大小寫不匹配 |
| ^~ | 普通字符匹配:使用前綴匹配,如果匹配成功,則不再匹配其他location |
| = | 普通字符精確匹配,也就是完全匹配 |
| @ | 定義一個命名的location,使用在內部定向時 |
2.2.2、location優先級
- 相同型別的運算式,字串長的會優先匹配
- 按優先級排列
① = 型別
② ^~型別運算式
③ 正則運算式 (~和 ~*) 型別
④常規字串匹配型別,按前綴匹配
⑤ 通用匹配(/),如果沒有其他匹配,任何請求都會匹配到
2.2.3、location優先級規則
- 匹配某個具體檔案
(location=完整路徑) > (location ~ 完整路徑) > (location ~ *完整路徑) >(location~完整路徑) > (location完整路徑) > (location /) - 用目錄做匹配訪問某個檔案
(location=目錄) > (location ^ ~ 目錄/) > (location ~ 目錄 ) > (location~*目錄) > (location目錄) > (location /)
三、Rewrite使用場景實驗
搭建基礎環境
(1)、先把Nginx搭建好!
[root@localhost opt]# tar xzvf nginx-1.15.9.tar.gz
[root@localhost nginx-1.15.9]# useradd -M -s /bin/nologin nginx
[root@localhost opt]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@localhost opt]# cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make -j3 && make install
#########優化路徑#########
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx
[root@localhost nginx-1.15.9]# nginx -t ### 檢查組態檔
#########啟動、重新配置、停止Nginx############
[root@localhost nginx-1.15.9]# nginx
[root@localhost nginx-1.15.9]# netstat -anpt |grep nginx
[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# killall -s QUIT nginx
#########添加Nginx系統服務##########
[root@localhost ~]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
[root@localhost ~]# systemctl start nginx
(2)、環境搭建
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top; ### 更改域名
#charset koi8-r;
access_log /var/log/nginx/www.51xit.top.access.log; ###改訪問日志地址
location / {
root html;
index index.html index.htm;
}
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# touch /var/log/nginx/www.51xit.top.access.log
測驗:打開網頁www.51xit.top 顯示正常

(3)、基于域名的跳轉
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset koi8-r;
access_log /var/log/nginx/www.51xit.top.access.log;
location / {
root html;
index index.html index.htm;
if ($host = 'www.51xit.top') ###這邊改一下本機域名
{
rewrite ^/(.*)$ http://www.52xit.top/$1 permanent; ###這邊改下跳轉后的域名
}
}
測驗:打開瀏覽器輸入www.51xit.top它會跳轉到www.51xit.top中去


(3)、基于客戶端IP地址的跳轉
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /var/log/nginx/www.51xit.top.access.log;
set $rewrite true;
if($remote_addr = "20.0.0.6") {
set $rewrite false;
access_log /var/log/nginx/www.51xit.top.access.log;
set $rewrite true;
if($remote_addr = "20.0.0.6"){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /wh.html;
}
location = /wh.html {
root /usr/local/nginx/html/;
}
location / {
root html;
index index.html index.htm;
}
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# cp index.html wh.html
[root@localhost html]# vi wh.html ###里面稍微修改一下,可以改成維護中,
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
測驗:客戶端瀏覽器輸入www.51xit.top自動跳轉到維護頁面

(4)、基于舊域名跳轉到新域名后面加目錄
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top; ###改域名
charset utf-8; ###改字符集
access_log /var/log/nginx/www.51xit.top.access.log; ###改日志存盤路徑,去掉main
location /bbs {
rewrite (.+) http://www.52xit.top/new$1 permanent;
}
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
測驗:客戶端瀏覽器輸入www.51xit.top/bbs自動跳轉新域名www.52xit.top/bbs


(5)、基于引數匹配的跳轉
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /var/log/nginx/www.51xit.top.access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.51xit.top permanent;
}
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
測驗:客戶端瀏覽器輸入www.51xit.top/100-100-100.html 頁面顯示www.51xit.top
客戶端瀏覽器輸入www.51xit.top/100-200-100.html 頁面顯示www.51xit.top
客戶端瀏覽器輸入www.51xit.top/100-200-10dsa0.html 頁面不能跳轉,因為有英文字符了,不是數字

(5)、基于目錄下所有php結尾的檔案跳轉,訪問www.51xit.top/upload/1.php跳轉到首頁
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /var/log/nginx/www.51xit.top.access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.51xit.top permanent;
}
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
測驗:訪問www.51xit.top/upload/1.php跳轉到www.51xit.top
(6)、基于最普通一條url請求的跳轉,訪問一個具體的頁面跳轉到首頁
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /var/log/nginx/www.51xit.top.access.log;
location ~* ^/1/test.html {
rewrite (.+) http://www.51xit.top permanent;
}
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
測驗:訪問www.51xit.top/1/test.html跳轉到www.51xit.top首頁上
(7)、基于報錯代碼來跳轉的
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /var/log/nginx/www.51xit.top.access.log;
error_page 500 502 503 504 /index.html;
location = /index.html {
root html;
}
[root@localhost html]# systemctl restart nginx.service ###重啟nginx服務
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/1344.html
標籤:其他
下一篇:維護Nginx網頁時如何跳轉維護頁面?新舊域名如何跳轉?-------總結了下nginx rewrite 網頁跳轉!
