1、rewrite基本概述1.1、什么是rewriterewrite主要實現(xiàn)URL地址重寫,以及重定向,就是把傳入web的請求重定向到其他url的過程。 1.2、rewrite使用場景
1.3、rewrite配置示例句法:Syntax: rewrite regex replacement [flag] 默認:Default: -- 語境:Context: server,location,if
#用于切換維護頁面場景 #rewrite ^(.*)$ /page/maintain.html break; 2、rewrite標記Flagrewrite指令根據(jù)表達式來重定向URL,或者修改字符串,可以應用于server,location,if環(huán)境下,每行rewrite指令最后跟一個flag標記,支持的flag標記有如下表格所示:
2.1、last與break區(qū)別對比示例[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf 1 server { 2 listen 80; 3 server_name rewrite.; 4 root /www/rewrite; 5 6 location ~ ^/break { 7 rewrite ^/break /test/ break; 8 } 9 10 location ~ ^/last { 11 rewrite ^/last /test/ last; 12 } 13 14 location /test/ { 15 default_type application/json; 16 return 200 "ok"; 17 } 18 } ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "/etc/nginx/conf.d/rewrite.conf" 18L, 296C written [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~]# systemctl restart nginx 如果懂shell腳本的,這兩個就類似于腳本中的,break和continue 2.2、瀏覽器訪問break
2.3、瀏覽器訪問last
2.4、last與break區(qū)別break 只要匹配到規(guī)則,則會去本地配置路徑的目錄中尋找請求的文件; 而last只要匹配到規(guī)則,會對其所在的server(...)標簽重新發(fā)起請求。 break請求: 1、請求rewrite./break 2、首先:會去查找本地的/code/test/index.html; 3、如果找到了,則返回/code/test/index.html的內(nèi)容; 4、如果沒找到該目錄則報錯404,如果找到該目錄沒找到對應的文件則403
last請求: 1、請求rewrite./last 2、首先:會去查找本地的/code/test/index.html; 3、如果找到了,則返回/code/test/index.html的內(nèi)容; 4、如果沒找到,會對當前server重新的發(fā)起一次請求,rewrite./test/ 5、如果有l(wèi)ocation匹配上,則直接返回該location的內(nèi)容。 4、如果也沒有l(wèi)ocation匹配,再返回404; 所以,在訪問/break和/last請求時,雖然對應的請求目錄/test都是不存在的,理論上都應該返回404,但是實際上請求/last的時候,是會有后面location所匹配到的結(jié)果返回的,原因在于此。 2.5、redirect與permanent區(qū)別對比示例[root@web01 ~]# cat /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name rewrite.; root /www/rewrite;
location /test { rewrite ^(.*)$ https://blog. redirect; #rewrite ^(.*)$ https://blog. permanent; #rewrite 301 https://blog.; #rewrite 302 https://blog.; } } [root@web01 ~]# cat /etc/nginx/conf.d/blog.conf server { listen 80; server_name blog.;
location / { root /www/blog; index index.html; } } [root@web01 ~]# mkdir /www/blog [root@web01 ~]# echo "blog" > /www/blog/index.html [root@web01 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.0.207 blog. 2.6、redirect與permanent區(qū)別(實現(xiàn)https)redirect: 每次請求都會詢問服務器,如果當服務器不可用時,則會跳轉(zhuǎn)失敗 permanent: 第一次請求會詢問,瀏覽器會記錄跳轉(zhuǎn)的地址,第二次則不再詢問服務器,直接通過瀏覽器緩存的地址跳轉(zhuǎn)。 3、rewrite規(guī)則實踐在寫rewrite規(guī)則之前,我們需要開啟rewrite日志對規(guī)則的匹配進行調(diào)試。 3.1、案例一用戶訪問/abc/1.html實際上真實訪問的是/ccc/bbb/2.html #http://www./abc/1.html ==> http://www./ccc/bbb/2.html #1.準備真實訪問路徑 [root@web01 ~]# mkdir /www/ccc/bbb -p [root@web01 ~]# echo "ccc_bbb_2" > /www/ccc/bbb/2.html
#2.Nginx跳轉(zhuǎn)配置 [root@web03 ~]# cd /etc/nginx/conf.d/ [root@web01 conf.d]# vim ccbb.conf server { listen 80; server_name rewrite1.; location / { root /www; index index.html; } location /abc { rewrite (.*) /ccc/bbb/2.html redirect; #return 302 /ccc/bbb/2.html; } }
#3.重啟Nginx服務 [root@web01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 conf.d]# systemctl restart nginx 3.2、案例二用戶訪問/2018/ccc/2.html實際上真實訪問的是/2014/ccc/bbb/2.html #1.準備真是的訪問路徑 [root@web01 conf.d]# mkdir /www/2021/ccc/bbb -p [root@web01 conf.d]# echo "2021_ccc_bbb_2"> /www/2021/ccc/bbb/2.html
#2.Nginx跳轉(zhuǎn)配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; server_name rewrite1.; location / { root /code; index index.html; } location /2020 { rewrite ^/2020/(.*)$ /2021/$1 redirect; } }
#3.重啟nginx服務 [root@web03 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web03 conf.d]# nginx -s reload 3.3、案例三用戶訪問/test實際上真實訪問的是https://www.baidu.com #1.Nginx跳轉(zhuǎn)配置 [root@web03 conf.d]# cat test.conf server { listen 80; server_name rewrite1.; location /test { rewrite (.*) https://www.baidu.com redirect; } }
#2.重啟nginx服務 [root@web01 conf.d]# systemctl restart nginx 3.4、案例四用戶訪問course-11-22-33.html實際上真實訪問的是/course/11/22/33/course_33.html #1.準備真是的訪問路徑 [root@web01 conf.d]# mkdir /www/course/11/22/33 -p [root@web01 conf.d]# echo "curl sun.com" > /www/course/11/22/33/course_33.html
#2.Nginx跳轉(zhuǎn)配置 [root@web01 conf.d]# vim test.conf server { listen 80; server_name test1.sun.com; root /www; index index.html; location / { rewrite ^/course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect; #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect; } }
#3.重啟nginx服務 [root@web01 conf.d]# systemctl restart nginx 3.5、案例五將http請求跳轉(zhuǎn)https #Nginx跳轉(zhuǎn)配置 server { listen 80; server_name www.sun.com; rewrite ^(.*) https://$server_name$1 redirect; #return 302 https://$server_name$request_uri; }
server { listen 443; server_name blog.sun.com; ssl on; } 4、rewrite場景示例[root@web01 conf.d]# vim discuz.sun.com server { listen 80; server_name discuz.sun.com; location / { root /www; index index.php index.html; } location ~ \.php$ { root /www; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcg i_script_name; include fastcgi_params; } } [root@web01 conf.d]# mkdir /www/–p [root@web01 discuz]# unzip Discuz_X3.4_SC_UTF8_20210119.zip [root@web01 discuz]# chown -R www.www /www/ [root@web01 discuz]# chmod 777 upload/ MariaDB [(none)]> create database discuz; Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123'; Query OK, 0 rows affected (0.00 sec)
server { listen 80; server_name discuz.;
location / { root /code/discuz/upload; index index.php index.html; rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last; rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last; rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last; if (!-e $request_filename) { return 404; } }
location ~ \.php$ { root /code/discuz/upload; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5、Rewrite規(guī)則補充5.1、Rewrite匹配優(yōu)先級1.先執(zhí)行server塊的rewrite指令 2.其次執(zhí)行l(wèi)ocation匹配規(guī)則 3.最后執(zhí)行l(wèi)ocation中的rewrite
5.2、Rewrite與Nginx全局變量Rewrite在匹配過程中,會用到一些Nginx全局變量
$server_name #當前用戶請求的域名
server { listen 80; server_name test.; rewrite ^(.*)$ https://$server_name$1; } $request_filename 請求的文件路徑名(帶網(wǎng)站的主目錄/code/images/test.jpg) $request_uri 當前請求的文件路徑(不帶網(wǎng)站的主目錄/inages/test.jpg)
#大多數(shù)用于http協(xié)議轉(zhuǎn)gttps協(xié)議 server { listen 80; server_name php.; return 302 https://$server_name$request_uri; } $scheme 用的協(xié)議,比如http或者https 5.3、如何更加規(guī)范的書寫Rewrite規(guī)則server { listen 80; server_name www. ; if ($http_host = ){ rewrite (.*) http://www.$1; } }
#推薦書寫格式 server { listen 80; server_name ; rewrite ^ http://www.$request_uri; } server { listen 80; server_name www.; } |
|