本站教程收集整理的这篇文章主要介绍了第十四章?Nginx实现Rewrite重写,本站教程本站觉得挺不错的,现在分享给大家,也给大家做个参考。
1、rewrite基本概述
1.1、什么是rewrite
rewrite主要实现URL地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。
1.2、rewrite使用场景
- 地址跳转,用户访问www.sunedu.com这个URL是,将其定向至一个新的域名blog.sunedu.com
- 协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
- 伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
- 搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入
1.3、rewrite配置示例
句法:Syntax: rewrite regex @R_618_9363@cement [flag]
默认:Default: —
语境:Context: server,LOCATIOn,if
?
#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;
2、rewrite标记Flag
rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,LOCATIOn,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
Flag |
作用 |
Last |
本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break |
本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect |
返回302临时重定向,地址栏会显示跳转后的地址 |
peRMANent |
返回301永久重定向,地址栏会显示跳转后的地址 |
?
2.1、last与break区别对比示例
[root@web01 ~]# vim /etc/Nginx/conf.d/rewrite.conf
1 server {
2 listen 80;
3 server_name rewrite.sunedu.com;
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请求:
便宜美国vps1、请求rewrite.sunedu.com/break
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403
?
last请求:
1、请求rewrite.sunedu.com/last
2、首先:会去查找本地的/code/test/index.html;
3、如果找到了,则返回/code/test/index.html的内容;
4、如果没找到,会对当前server重新的发起一次请求,rewrite.sunedu.com/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.sunedu.com;
????root /www/rewrite;
????
????LOCATIOn /test {
????rewrite ^(.*)$ https://blog.sunedu.com redirect;
????#rewrite ^(.*)$ https://blog.sunedu.com peRMANent;
????#rewrite 301 https://blog.sunedu.com;
????#rewrite 302 https://blog.sunedu.com;
????}
}
[root@web01 ~]# cat /etc/Nginx/conf.d/blog.conf
server {
????listen 80;
????server_name blog.sunedu.com;
?
????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.sunedu.com
2.6、redirect与peRMANent区别(实现https)
redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败
peRMANent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。
3、rewrite规则实践
在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。
3.1、案例一
用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
#http://www.sunedu.com/abc/1.html ==> http://www.sunedu.com/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.sunedu.com;
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.sunedu.com;
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实际上真实访问的是@L_419_1@
#1.Nginx跳转配置
[root@web03 conf.d]# cat test.conf
server {
listen 80;
????????server_name rewrite1.sunedu.com;
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/
@H_563_110@mariaDB [(nonE)]> create database discuz;
Query OK, 1 row affected (0.00 seC)
?
@H_563_110@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.drz.com;
?
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.drz.com;
rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg)
?
#大多数用于http协议转gttps协议
server {
listen 80;
server_name php.drz.com;
return 302 https://$server_name$request_uri;
}
$scheR_307_11845@e 用的协议,比如http或者https
5.3、如何更加规范的书写Rewrite规则
server {
listen 80;
server_name www.drz.com drz.com;
if ($http_host = drz.com){
rewrite (.*) http://www.drz.com$1;
}
}
?
#推荐书写格式
server {
listen 80;
server_name drz.com;
rewrite ^ http://www.drz.com$request_uri;
}
server {
listen 80;
server_name www.drz.com;
}
本站总结
以上是本站教程为你收集整理的第十四章?Nginx实现Rewrite重写全部内容,希望文章能够帮你解决第十四章?Nginx实现Rewrite重写所遇到的程序开发问题。
如果觉得本站教程网站内容还不错,欢迎将本站教程推荐给好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。