这里写目录标题
一、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重写以及重定向。![在这里插入图片描述](https://img-blog.csdnimg.cn/20201120111016433.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80ODE4NTY0Mw==,size_16,color_FFFFFF,t_70#pic_center)
Nginx跳转需求的实现方式
①使用rewrite进行匹配跳转
②使用if匹配全局变量后跳转
③使用location匹配在跳转
二、Nginx正则表达式
- 常用的正则表达式元字符
^:匹配输入字符串的起始位置
$:匹配输入字符串的结束位置
*****:匹配前面的字符零次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符零次或一次
.:匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符
****:转义符
\d:匹配纯数字
{n}:重复n次
{n,}:重复n次或更多次
[c]:匹配单个字符c
[a-z]:匹配a-z小写字母的任意一个
[a-zA-Z]:匹配a-z小写字母或A-Z大写字母的任意一个
三、Rewrite命令
3.1 语法命令:
3.2 flag标记说明:
3.3、last和break比较:
1)、last:url重写后,马上发起一个新请求。再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变。
2)、break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变。
总结:last和break在重定向后,地址栏都不会发生变化,这是它们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配。
四、location介绍
4.1 location的优先级
相同类型的表达式,字符串长的会优先匹配
按优先级排列
① = 类型
② ^~类型表达式
③ 正则表达式 (~和 ~*) 类型
④常规字符串匹配类型,按前缀匹配
⑤ 通用匹配(/),如果没有其他匹配,任何请求都会匹配到
4.2 rewrite和location的比较
1、相同点
都能实现跳转
2、不同点
rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
3、rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
四、六种重定向实验
实验环境准备
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top; ## 修改域名
#charset koi8-r;
## 指定 www.51xit.top 的日志文件
access_log /var/log/nginx/www.51xit.top.access.log; ## 修改此处
location / {
root html;
index index.html index.htm;
}
== >> wq 保存
[root@localhost ~]# mkdir -p /var/log/nginx ##创建存放日志文件的目录
[root@localhost ~]# touch /var/log/nginx /www.51xit.top.access.log ##存放www.51xit.top日志的文件
[root@localhost nginx]# ll
total 8
-rw-r--r-- 1 root root 5973 Sep 7 07:01 www.51xit.top.access.log ##查看文件生成成功
[root@localhost ~]# nginx -t ##检测语法有无错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx ##刷新nginx配置
4.1、基于域名的跳转
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
#### 新增部分 ####
if ($host = 'www.51xit.top') { ##这边是老域名
rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
##使用正则表达式匹配 “/开头和任意字符结尾”,然后跳转到新的www.52xit.top
}
}
[root@localhost ~]# nginx -t ##查看语法有没有错误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx
打开浏览器输入 www.51xit.top 跳转到 www.52xit.top
4.2、基于客户端IP访问跳转
基于客户端IP访问跳转。例如今天公司业务新版本上线,所有IP访问任何都显示一个固定维护页面,只有公司IP才能访问正常
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8; ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉
access_log /var/log/nginx/www.51xit.top.access.log;
set $rewrite true; ## 新增的
if ($remote_addr = '20.0.0.13') { ## 我们这边设置的是管理员IP
set $rewrite false; ##管理员IP设为 flase
}
if ($rewrite = true) { ## 如果匹配的不是 20.0.0.13
rewrite (.+) /wh.html; ## 跳转到 维护网页
}
location = /wh.html { ## 设置维护网页的结尾域名
root /usr/local/nginx/html/; ## 维护网页存放的路径
}
location / { ## 之前在这里做的配置加上 # 号
root html;
index index.html index.htm;
# if ($host = 'www.51xit.top') {
# rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
# }
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ll
total 8
-rw-r--r-- 1 root root 494 Sep 2 22:47 50x.html
-rw-r--r-- 1 root root 612 Sep 2 22:47 index.html
[root@localhost html]# cp index.html wh.html ##创建维护页面
[root@localhost html]# vi wh.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>网页维护中,暂时无法访问!!!</h1>
</body>
</html>
== >> wq 保存
[root@localhost html]# killall -s HUP nginx
测试:打开浏览器输入 20.0.0.25,会显示网页维护中,暂时无法访问
4.3、基于旧、新域名跳转并加目录
基于旧域名跳转到新域名后面加目录,录入现在访问的是 http://www.51xit.top/bbs
现在需要将这个域名下面的都转到 http://www.52xit.top/new/bbs/
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8; ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉
access_log /var/log/nginx/www.51xit.top.access.log;
####新增代码####
location /bbs { 输入http://www.51xit.top/bbs 跳转到 http://www.52xit.top/new/bbs/
rewrite (.+) http://www.52xit.top/new$1 permanent;
}
#######还是一样之前做的配置全部加上#号##########
# set $rewrite true;
# if ($remote_addr = '20.0.0.1') {
# set $rewrite false;
# }
# if ($rewrite = true) {
# rewrite (.+) /wh.html;
# }
# location = /wh.html {
# root /usr/local/nginx/html/;
# }
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx
测试:浏览器输入 www.51xit.top/bbs 会跳转到 www.51xit.top/new/bbs
4.4、基于参数匹配的跳转
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8; ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉
access_log /var/log/nginx/www.51xit.top.access.log;
##### 新增的 #####
## 输入 www.51xit.top/100-(100-200)—纯数字.html访问网页
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.51xit.top permanent;
}
######## 之前的加上#号 ##########
# location /bbs {
# rewrite (.+) http://www.52xit.top/new$1 permanent;
# }
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx
测试:在浏览器输入 www.51xit.top/100-200-100.html 测试成功
4.5、基于目录下所有php文件跳转
基于目录下所有以php结尾的文件跳转,访问http://www.51xit.top/upload/1.php跳转到首页
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8; ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉
access_log /var/log/nginx/www.51xit.top.access.log;
#### 新增内容 ####
### 输入 /upload/任意字符.php结尾 来访问网页
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.51xit.top permanent;
}
######## 之前的加上#号 ##########
#if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
#rewrite (.*) http://www.51xit.top permanent;
#}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx
测试:在浏览器输入http://www.51xit.top/upload/bbs/1.php
4.6、基于最普通url请求的跳转
基于普通一条url请求的跳转,访问一个具体的页面跳转到首页
[root@localhost html]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.51xit.top;
charset utf-8; ## 拆成 utf-8(网页内显示中文), 并且 # 号去掉
access_log /var/log/nginx/www.51xit.top.access.log;
#### 新增内容 ####
### 输入 /1/test.html 结尾访问页面
location ~* ^/1/test.html {
rewrite (.+) http://www.51xit.top permanent;
}
######## 之前的加上#号 ##########
# location ~* /upload/.*\.php$ {
# rewrite (.+) http://www.51xit.top permanent;
# }
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# killall -s HUP nginx