常用的nginx正则表达式 | 说明 |
---|---|
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll” |
+ | 匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o” |
? | 匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}” |
. | 匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“$”则匹配“$” |
\d | 匹配纯数字[0-9] |
\s | 空白符 |
\w | 任意单词字符包括下划线[A-Za-z0-9_] |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
{n,m} | 重复n到m次 |
[] | 定义匹配的字符范围 |
[c] | 匹配单个字符c |
[a-z] | 匹配a-z小写字母的任意一个 |
[a-zA-Z0-9] | 匹配所有大小写字母或数字 |
() | 表达式的开始和结束位置 |
| | 或运算符 |
从功能看rewrite和location似乎有点像,都能实现跳转,主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,还可以proxy_pass 到其他机器
rewrite | location |
---|---|
对访问的域名或者域名内的URL路径地址重写 | 对访问的路径做访问控制或者代理转发 |
~
或~*location = / {}
###=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/
location / {}
###因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但后面前缀路径会和最长字符串优先匹配(最长匹配)
location /documents/ {}
###匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它location只有其它location后面的前缀路径没有匹配到时,才会采用这一条
location /documents/abc {}
###匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它location后面的前缀路径没有匹配到时,才会采用这一条
location ^~ /images/ {}
###匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
location ~* \.(gif|jpg|jpeg)$ {}
###匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
location /images/abc {}
###最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
location ~ /images/abc {}
###匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
location /images/abc/1.html {}
###匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高
==(location = 完整路径) > (location ^~ 路径) > (location ~
,~ 正则顺序) > (location 部分前缀路径) > (location /)==*
在没有精准匹配的情况下,先看前缀匹配的长度,然后根据最长的前缀匹配的优先级去确定是否再去看其它正则匹配location
如果最长的前缀匹配带有 ^~ 则不再看其它正则匹配location,如果最长的前缀匹配是没有修饰符的一般匹配则会再看其它正则匹配location,前缀匹配看长度,最长的优先匹配
正则匹配看上下顺序,由上往下依次匹配,当有匹配成功时候,停止匹配,按当前匹配规则处理请求
只有在精准、前缀、正则、一般 都没有匹配到的时候才会看通用匹配
通过域名访问网站首页比较频繁,使用这个会加速处理,比如说官网。
可以是一个静态首页,也可以直接转发给后端应用服务器
location = /index.html {
root html;
index index.html index.htm;
}
这是nginx作为http服务器的强项
有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
比如用来转发带.php、.jsp后缀的动态请求到后端应用服务器
非静态文件请求就默认是动态请求
location / {
proxy_pass http://tomcat_server;
}
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向
比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求
rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用
例如 http://www.xc.com/abc/bbs/index.php?a=1&b=2 只对/abc/bbs/index.php重写
Nginx:通过ngx_http_rewrite_module模块支持URL重写、支持if条件判断,但不支持else
跳转:从一个location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
PCRE支持:perl兼容正则表达式的语法规则匹配
重写模块set指令:创建新的变量并设其值
rewrite [flag];
###regex :表示正则匹配规则
###replacement :表示跳转后的内容
###flag :表示rewrite支持的flag标记
现在公司旧域名www.xc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xc.com; ###域名修改
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log; ###日志修改
location / { ###添加域名重定向
if ($host = 'www.xc.com'){ ###$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.benet.com/$1 permanent; ###$1为正则匹配的内容,即“域名/”之后的字符串
}
root html;
index index.html index.htm;
}
}
echo "192.168.230.3 www.xc.com www.benet.com" >> /etc/hosts
systemctl restart nginx
浏览器输入模拟访问 http://www.xc.com/test/1.html(虽然这个请求内容是不存在的)
会跳转到www.benet.com/test/1.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转
今天公司业务新版本上线,要求所有IP访问任何内容都显示一个固定维护页面,只有公司 IP :192.168.230.3访问正常
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xc.com;
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log;
###设置是否合法的IP标记
set $rewrite true; ###设置变量$rewrite,变量值为boole值true
###判断是否为合法IP
if ($remote_addr = "192.168.230.3"){ ###当客户端IP为192.168.230.3时,将变量值设为false,不进行重写
set $rewrite false;
}
###除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ ###当变量值为true时,进行重写
rewrite (.+) /weihu.html; ###将域名后边的路径重写成/weihu.html后转发,例如www.xc.com/weihu.html
}
location = /weihu.html {
root /var/www/html; ###网页返回/var/www/html/weihu.html的内容
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/html/
echo "We are maintaining now!
" > /var/www/html/weihu.html
systemctl restart nginx
只有IP为192.168.230.3能正常访问,其它地址都是维护页面
注:
如果rewrite (.+) /weihu.html; 换成rewrite (.+) /weihu.html permanent; 的话,若不是 192.168.230.3的主机访问会使浏览器修改请求访问的URL成 http://www.xc.com/weihu.html 再请求访问,这样就会进入一直在 rewrite 的死循环,访问请求会一直被重写成 http://www.xc.com/weihu.html 再请求访问
现在访问的是 http://bbs.xc.com/post/,现在需要将这个域名下面的访问都跳转到http://www.xc.com/bbs/post/
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.kgc.com www.xc.com;
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log;
###添加
location /post {
rewrite (.+) http://www.xc.com/bbs$1 permanent; ###这里的$1为位置变量,代表/post
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.230.3 bbs.xc.com" >> /etc/hosts
systemctl restart nginx
使用浏览器访问 http://bbs.xc.com/post/1.html 跳转到 http://www.xc.com/bbs/post/1.html
现在访问http://www.xc.com/100-(100|200)-100.html 跳转到http://www.xc.com页面
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xc.com;
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.xc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
使用浏览器访问http://www.xc.com/100-200-100.html或http://www.xc.com/100-100-100.html跳转到http://www.xc.com页面
$request_uri:包含请求参数的原始URI,不包含主机名,如:http://www.xc.com/abc/bbs/index.html?a=1&b=2 中的 /abc/bbs/index.php?a=1&b=2
$uri:这个变量指当前的请求URI,不包括任何参数,如:/abc/bbs/index.html
$document_uri:与$uri相同,这个变量指当前的请求URI,不包括任何传递参数,如:/abc/bbs/index.html
要求访问 http://www.xc.com/upload/123.php 跳转到首页
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xc.com;
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.xc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏览器访问 http://www.xc.com/upload/123.php 跳转到http://www.xc.com页面
要求访问一个具体的页面如 http://www.xc.com/abc/123.html 跳转到首页
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.xc.com;
charset utf-8;
access_log /var/log/nginx/www.xc.com-access.log;
location ~* ^/abc/123.html {
rewrite (.+) http://www.xc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏览器访问 http://www.xc.com/abc/123.html 跳转到http://www.xc.com页面