Rewrite主要實現url地址重寫, 以及地址重定向,就是將用戶請求web服務器的地址從新定向到其餘URL的過程。html
#rewrite表達式能夠應用在server,location, if標籤下 Syntax: rewrite regex replacement [flag]; Default: -- Context: server, location, if #用於切換維護頁面場景 #rewrite ^(.*)$ /page/wh.html break;
rewrite指令根據表達式來重定向URI,或者修改URI字符串。
每行rewrite指令最後跟一個flag標記,支持的flag標記有以下表格所示:nginx
flag | |
---|---|
last | 本條規則匹配完成後,中止匹配,不在匹配後面的規則 |
break | 本條規則匹配完成後,中止匹配,不在匹配後面的規則 |
redirect | 返回302臨時重定向, 地址欄會顯示跳轉後的地址 |
permanent | 返回301永久重定向, 地址欄會顯示跳轉後的地址 |
[root@bgx conf.d]# cat rewrite.conf server { listen 80; server_name rewrite.oldboy.com; root /code; location ~ ^/break { rewrite ^/break /test/ break; } location ~ ^/last { rewrite ^/last /test/ last; } location /test/ { return 200 'ok'; } } #重啓Nginx服務 [root@bgx conf.d]# systemctl restart nginx
break匹配到規則,則會去本地路徑中目錄中尋找對應請求的文件。
last匹配到規則,會對其所在的server{...}標籤從新發起請求。
因此,在訪問/break和/last請求時,雖然對應的請求目錄/test都是不存在了,理論上都應該返回404,可是實際請求/last的時候,是會有後面localtion所匹配到的結果返回的,若是last匹配不到location的結果則在返回錯誤。web
[root@Nginx ~]# cat /etc/nginx/conf.d/rewrite.conf server { listen 80; server_name rewrite.oldboy.com; root /code; location ~ ^/bgx { rewrite ^(.*)$ https://www.xuliangwei.com redirect; rewrite ^(.*)$ https://www.xuliangwei.com permanent; #return 301 http://kt.xuliangwei.com; #return 302 http://kt.xuliangwei.com; } }
http://www.bgx.com/abc/1.html ==> http://www.bgx.com/ccc/bbb/2.html瀏覽器
#1.準備真實的訪問路徑 [root@web03 ~]# mkdir /code/ccc/bbb -p [root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html #2.Nginx跳轉配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; location / { root /code; index index.html; } location /abc { rewrite (.*) /ccc/bbb/2.html redirect; #return 302 /ccc/bbb/2.html; } } #3.重啓Nginx服務 [root@web03 ~]# systemctl restart nginx
http://www.bgx.com/2018/ccc/bbb/2.html ==> http://www.bgx.com/2014/ccc/bbb/2.html安全
#1.準備真實的訪問路徑 [root@web03 ~]# mkdir /code/2014/ccc/bbb -p [root@web03 ~]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html #2.Nginx跳轉配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; location / { root /code; index index.html; } location /2018 { rewrite ^/2018/(.*)$ /2014/$1 redirect; } } #3.重啓Nginx服務 [root@web03 ~]# systemctl restart nginx
location /test { rewrite (.*) http://www.xuliangwei.com redirect; }
http://www.bgx.com/course-11-22-33.html ==> http://www.bgx.com/course/11/22/33/course_33.html服務器
#1.準備真實的訪問路徑 [root@web03 ~]# mkdir /code/course/11/22/33/ -p [root@web03 ~]# echo "Curl docs.etiantian.org" > /code/course/11/22/33/course_33.html #2.Nginx跳轉配置 [root@web03 conf.d]# cat ccbb.conf server { listen 80; root /code; 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@web03 ~]# systemctl restart nginx
server { listen 80; server_name bgx.com; rewrite ^(.*) https://$server_name$1 redirect; #return 302 https://$server_name$request_uri; } server { listen 443; server_name bgx.com; ssl on; }
$server_name 當前用戶請求的域名
$request_filename 當前請求的文件路徑名(帶網站的主目錄/code/images/test.jpg)
$request_uri 當前請求的文件路徑名(不帶網站的主目錄/images/test.jpg)
$scheme用的協議,好比http或者https測試
server { listen 80; server_name www.oldboyedu.com oldboyedu.com; if ($http_host = oldboyedu.com){ rewrite (.*) http://www.nginx.org$1; } } #推薦的書寫格式 server { listen 80; server_name oldboyedu.com; rewrite ^ http://www.oldboyedu.com$request_uri; } server { listen 80; server_name www.oldboyedu.com; }