详解nginx的rewrite模块
一:nginx rewrite概述
Nginx rewrite 主要功能是实现URL地址重写。Nginx的 rewrite 规则需要PCRE软件的支持,即通过PCRE兼容正则表达式语法进行规则匹配。
二:nginx跳转实现
三:rewrite实际场景
- nginx跳转需求的实现方式
使用rewrite实现匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转 - rewrite放在server{}、if{}、location{}段中
- location只对域名后面的除去传递参数外的字符串起作用
- 对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理
四:nginx正则表达式
常用的正则表达式元字符
字符 | 说明 |
---|---|
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一次 |
. | 匹配出"\n"之外的任意单个字符 |
\ | 表示转义 |
\d | 匹配纯数字 |
{n} | 匹配前面的字符重复n次 |
{n,} | 匹配前面的字符重复至少n次 |
{n,m} | 匹配前面的字符重复n到m次 |
[a-z] | 匹配a-z小写字母的任意一个 |
五:rewrite命令
- rewrite命令语法
rewrite < regex > < replacement > [flag];
regex:正则
replacement:重写后的URL
flag:重写状态标记 - flag标记说明
flag标记符号 | 说明 |
---|---|
last | 本条规则匹配完成后,继续向下匹配新的location URL规则 |
break | 本条规则匹配完成即终止,不再匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址,爬虫不会更新URL |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址 ,爬虫更新URL |
- last和break比较
last | break | |
---|---|---|
使用场景 | 一般写在server和if中 | 一般使用在location中 |
URL匹配 | 不终止重写后的URL匹配 | 终止重写后的URL匹配 |
六:location分类与优先级
location分类:
- location = patt {} [精准匹配]
- location patt {} [一般匹配]
- location ~ patt {} [正则匹配]
正则匹配的常用表达式:
标记 | 说明 |
---|---|
~ | 执行一个正则匹配,区分大小写 |
~* | 执行一个正则匹配,不区分大小写 |
!~ | 执行一个正则匹配,区分大小写不匹配 |
!~* | 执行一个正则匹配,不区分大小写不匹配 |
^~ | 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location |
= | 普通字符精准匹配。也就是完全匹配 |
@ | 定义一个命名的location,使用在内部定向时 |
location优先级:
- 相同类型的表达式,字符串长的会优先匹配
- 按优先级排序
= 类型 > ^~类型 > 正则表达式( ~ 和~*)类型 > 常规字符串类型 > 通用匹配 (/),如果没有其他匹配,任何请求都会匹配到
location优先级规则:
- 匹配某个具体文件
(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)>(location ~ 完整路径)>(location 完整路径)>(location /) - 用目录做匹配访问某个文件
(location = 完整路径)>(location ^~ 完整路径)>(location ~ 完整路径)>(location ~* 完整路径)>(location 完整路径)>(location /)
七:使用场景
7.1:首先安装nginx服务
- 安装nginx源
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安装nginx软件包
[root@localhost ~]# yum install nginx -y
- 启动nginx
[root@localhost conf.d]# systemctl start nginx
7.2:基于域名的跳转
公司旧域名www.kgc.com,因业务需求变更,需要使用新域名www.newkgc.com代替
要求:不能废除旧域名,从旧域名跳转到新域名,且保持其参数不变
- 首先需要安装DNS服务解析两个域名
- 修改nginx的配置文件
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
# 加入if判断,用rewrite进行跳转
location / {
if ($host = "www.kgc.com") {
rewrite ^/(.*)$ http://www.newkgc.com/$1 permanent;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
- windows主机验证
7.3:基于客户端IP访问跳转
公司业务版本上线,所有IP访问任何内容都显示错误页面,只有公司IP访问正常
- 修改nginx的配置文件
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
# 设置是否为合法的IP标志
set $IP true;
# 判断是否为合法IP
if ($remote_addr = "192.168.209.133") {
set $IP false;
}
# 非法IP判断,进行跳转
if ($IP = true) {
rewrite (.+) /error.html;
}
# 跳转站点位置
location /error.html {
root /usr/share/nginx/html;
}
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# echo "Website is error,Please visit later." > /usr/share/nginx/html/error.html
[root@localhost html]# systemctl restart nginx
- Windows主机验证
7.4:基于旧、新域名跳转并加目录
例如访问http://bbs.kgc.com 将这个域名下面的发帖都跳转到 http://www.kgc.com/bbs,注意保持域名跳转后的参数不变
- 修改nginx的配置文件
server {
listen 80;
server_name bbs.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
location /post {
rewrite (.+) http://www.kgc.com/bbs$1 permanent;
}
- Windows主机验证
7.5:基于参数匹配的跳转
访问http://www.kgc.com/100-200-1.html跳转到首页
- 修改nginx的配置文件
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
location / {
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.kgc.com permanent;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
- Windows主机验证
7.6:基于目录下所有php文件的跳转
访问http://www.kgc.com/upload/1.php跳转到首页
- 修改nginx的配置文件
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.kgc.com permanent;
}
- Windows主机验证
7.7:基于最普通url请求的跳转
要求:访问一个具体的页面跳转到首页
验证:游览器访问http://www.kgc.com/1/test.html跳转到首页
- 修改nginx的配置文件
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com.access.log main;
location ~* ^/1/test.html {
rewrite (.+) http://www.kgc.com permanent;
}
- Windows主机验证