1、rewrite基本概述1.1、什么是rewriterewrite主要实现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指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
2.1、last与break区别对比示例[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区别break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件; 而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。 break请求: 1、请求rewrite./break 2、首先:会去查找本地的/code/test/index.html; 3、如果找到了,则返回/code/test/index.html的内容; 4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403
last请求: 1、请求rewrite./last 2、首先:会去查找本地的/code/test/index.html; 3、如果找到了,则返回/code/test/index.html的内容; 4、如果没找到,会对当前server重新的发起一次请求,rewrite./test/ 5、如果有location匹配上,则直接返回该location的内容。 4、如果也没有location匹配,再返回404; 所以,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,原因在于此。 2.5、redirect与permanent区别对比示例[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区别(实现https)redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败 permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。 3、rewrite规则实践在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。 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跳转配置 [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跳转配置 [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跳转配置 [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跳转配置 [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请求跳转https #Nginx跳转配置 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规则补充5.1、Rewrite匹配优先级1.先执行server块的rewrite指令 2.其次执行location匹配规则 3.最后执行location中的rewrite
5.2、Rewrite与Nginx全局变量Rewrite在匹配过程中,会用到一些Nginx全局变量
$server_name #当前用户请求的域名
server { listen 80; server_name test.; rewrite ^(.*)$ https://$server_name$1; } $request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg) $request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)
#大多数用于http协议转gttps协议 server { listen 80; server_name php.; return 302 https://$server_name$request_uri; } $scheme 用的协议,比如http或者https 5.3、如何更加规范的书写Rewrite规则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.; } |
|