前言:主播最近有点情绪波动,暂时没有前言拐友们,但在生产当中Nginx的rewrite功能相当重要,拐友们要好好看
目录
1.4.2rewrite放在server{}, if{},location{}段中
一.nginx rewrite概述
1.1概述
URL:指具体路径/位置
URI:指的是一个拥有相同类型/特性的对象集合
location是为了匹配访问的路径(URL)
if 做为分支判断,并且if分支,仅支持单分支
rewrite:对访问路径进行改写大分支:
1、改写的路径
在Nginx服务器上-改写路径
2、改写的路径
在后端/其他服务器上-跳转
1.2跳转场景
1.URL看起来更规范,合理
2.企业会将动态URL地址伪装静态地址提供服务
3.网址换新域名后,让旧的访问跳转到新的域名上
4.服务端某些业务调整
1.3 跳转实现
1.Nginx是通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。
2.另外该模块需要 PCRE支持,应在编译Nginx时指定PCRE 支持,默认已经安装。
3.根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后Nginx将返回500错误。
4.重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。
5.rewrite功能就是使用Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。
1.4Rewrite实际场景
1.4.1Nginx跳转需求的实现方式
1.使用rewrite进行匹配跳转(防盗链)
2.使用if匹配全局变量后跳转
3.使用location匹配再跳转
1.4.2rewrite放在server{}, if{},location{}段中
(多个server模块 sever模块里包含location location包含if if里面包含rewrite)
location只对域名后边的除去传递参数外的字符串起作用
1.4.3对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理
二.Nginx正则表达式
2.1.正则表达式概述
正则表达式:它是一个精准匹配,用户体验筛选的一种执行规范
2.2.常用的Nginx正则表达式
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll” |
+ | 匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o” |
? | 匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}” |
. | 匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$” |
\d | 匹配纯数字 |
{n} | 重复 n 次 |
{n,} | 重复 n 次或更多次 |
{n,m} | 重复 n 到 m 次 |
[] | 定义匹配的字符范围 |
[c] | 匹配单个字符 c |
[a-z] | 匹配 a-z 小写字母的任意一个 |
[a-zA-Z0-9] | 匹配所有大小写字母或数字 |
() | 表达式的开始和结束位置 |
| | 或运算符 |
ps:从功能看 rewrite 和 location 似乎有点像,都能实现跳转,主要区别在于 rewrite 是在同一域名内更改获取资源的路径,而 location 是对一类路径做控制访问或反向代理,还可以proxy_pass 到其他机器。
2.3正则表达的优势
1.精确匹配,减少遍历的消耗
2.节省服务响应的资源消耗
3.提高用户体验感
4.提高服务器并发时、处理效率/性能
2.4Nginx使用正则的作用
Nginx作为运维角度前段接受、想用客户请求的直接对服务器所以需要考虑到Nginx的执行效率(精确匹配-类比)、性能(资源消耗)、抗压能力(高并发时处理能力)、用户体验感(用户访问群体)
三.location
3.1location大致可以分为三类
- 精确匹配:location = / { … }
- 一般匹配:location / { … }
- 正则匹配:location ~ / { … }
3.2 location 常用的匹配规则
= | 进行普通字符精确匹配,也就是完全匹配 |
---|---|
^~ | 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location |
~ | 匹配大小写的匹配 |
~* | 不区分大小写的匹配 |
!~ | 区分大小写的匹配取非 |
!~* | 不区分大小写的匹配取非 |
3.3 location优先级
- 首先精确匹配 =
- 其次前缀匹配 ^~
- 其次是按文件中顺序的正则匹配 ~ 或 ~*
- 然后匹配不带任何修饰的前缀匹配
- 最后是交给 / 通用匹配
3.4.location示例说明
(1)location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。
(2)location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但若后面是正则表达式会和最长字符串优先匹配(最长匹配)
(3)location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
(4)location /documents/abc {} www.baidu.com
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
(7)location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高
优先级总结:
(location = 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (location /)
3.5location 匹配
- 首先看 优先级:精确>前缀>正则>一般>通用
- 优先级相同:正则看上下顺序,上面的优先;一般匹配看长度,最长匹配的优先
- 精确、前缀、正则、一般 都没有匹配到,最后再看通用匹配
3.6实际网站使用中,至少有三个匹配规则定义
3.6.1.第一个必选规则
- 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
- 可以是一个静态首页,也可以直接转发给后端应用服务器
location = / {
root html;
index index.html index.htm;
}
3.6.2.第二个必选规则
- 是处理静态文件请求,这是nginx作为http服务器的强项 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
3.6.3第三个规则
- 就是通用规则,比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
- 非静态文件请求就默认是动态请求
location / {
proxy_pass http://tomcat_server;
}
四.rewrite命令
4.1rewrite命令语法
rewrite < regex > < replacement > [flag]
regex:正则表达式
replacement :跳转后的内容
flag:rewrite支持的flag标记
last和break比较 | ||
last | break | |
使用场景 | 一般写在server和if中 | 一般使用在location中 |
URL匹配 | 不重质重写后的url匹配 | 终止重写后的url匹配 |
flag标记说明 | |
标记 | 说明 |
last | 相当于Apache的【L】标记,表示完成rewrite |
break | 本条规则匹配完成即终止,不在匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址,爬虫不会更新url |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url |
4.2rewrite执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
五.rewrite实例
5.1基于域名的跳转
- 现在公司旧域名www.kgc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.hehe.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.hehe.com-access.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.hehe.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.xinhehe.com/$1 permanent; #$1为正则匹配的内容,即“域名/”之后的字符串
}
root html;
index index.html index.htm;
}
echo "192.168.75.40 www.hehe.com" >> /etc/hosts
systemctl restart nginx
5.2基于客户端 IP 访问跳转
- 今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP 192.168.75.40访问正常。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.hehe.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.hehe.com-access.log; #日志修改
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.75.40"){ #当客户端IP为192.168.75.40时,将变量值设为false,不进行重写
set $rewrite false;
}
#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewirte (.+) /weihu.html; #将域名后边的路径重写成/weihu.html,例如www.hehe.com/weihu.html
}
location = /weihu.html {
root /var/www/html; #网页返回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
}
}
(.代表任意,+代表一次或多次)
mkidr -p /var/www/html/
ehho "<h1>We are maintaining now!</h1>" > /var/www/html/weihu.html
systemctl rsetart nginx
只有 IP 为 192.168.75.40 能正常访问,其它地址都是维护页面
--------------------------------------------------------
如果rewrite (.+) /weihu.html; 改成rewrite (.+) /weihu.html permanent; 的话,如果是非 192.168.75.40 的主机访问会使浏览器修改请求访问的URL成 http://www.hehe.com/weihu.html 再请求访问,这样就会进入一直在 rewrite 的死循环,访问请求会一直被重写成 http://www.hehe.com/weihu.html 再请求访问
mkdir -p /var/www/html/
echo 'hehe dao ci yi you' > /var/www/html/weihu.html
systemctl restart nginx
5.3基于旧域名跳转到新域名后面加目录
- 现在访问的是 http://bbs.hehe.com/post/1.html,现在需要将这个域名下面的访问都跳转到www.hehe.com/bbs/post/1.html
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.hehe.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.hehe.com-access.log;
#添加
locatoin /post {
rewirte (.+) http://www.hehe.com/bbs$1 permanent; #这里的$1为位置变量,代表/post
}
locaiton / {
root html;
index index.html index.htm;
}
}
mkdir -P /usr/local/nginx/html/bbs/post
echo "hehe zhen shuai" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.75.40 bbs.hehe.com" >> /etc/hosts
systemctl restart nginx
使用浏览器访问 http://bbs.hehe.com/post/1.html 跳转到 http://www.hehe.com/bbs/post/1.html
mkdir -p /usr/local/nginx/html/hehe/post
systemctl restart nginx.service
使用浏览器访问:
http://bbs.hehe.com/post/1.html跳转到https://www.hehe.com/bbs/post/1.html
5.4基于参数匹配的跳转
- 现在访问http://www.hehe.com/100-(100|200)-100.html 跳转到http://www.hehe.com页面
server {
listen 80;
server_name www.hehe.com; #域名修改
charset utf-8;
access_log /var/log/nginx/hehe.com.access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.hehe.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
浏览器访问: http://www.lic.com/100-200-100.html 转到http://www.hehe.com页面
5.5基于目录下所有 php 结尾的文件跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.hehe.com;
charset utf-8;
access_log /var/log/nginx/www.hehe.com.access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.hehe.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
echo "192.168.75.40 www.hehe.com" >> /etc/hosts
systemctl restart nginx
浏览器访问
http://www.hehe.com/upload/abc.php 跳转到http://www.hehe.com页面。
浏览器访问
http://www.hehe.com/upload/123.php 跳转到http://www.hehe.com页面
5.6基于最普通一条 url 请求的跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.hehe.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.hehe.com.access.log;
location ~* ^/abc/123.html {
rewrite (.+) http://www.hehe.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
浏览器访问
http://www.hehe.com/abc/123.html 跳转到www.hehe.com
六.总结
在生产环境中我们要掌握location的分类(精确匹配、一般匹配、正则匹配);掌握location的优先级(location = 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (location /);掌握rewrite的几种使用方法:基于域名的跳转、基于客户端 IP 访问跳转、基于旧域名跳转到新域名后面加目录、基于参数匹配的跳转、基于目录下所有 php 结尾的文件跳转、基于最普通一条 url 请求的跳转