Rewrite跳转场景
URL看起来更规范合理
企业会将动态URL地址伪装成静态地址提供服务
网址换新域名后,让旧的访问跳到新的域名上
服务端某些业务调整
Rewrite跳转实现
支持URL重写,支持if条件判断,但不自持else
循环最多可以执行10次超过后将返回500报错
rewrite使用Nginx全局变量或自己设置的变量,结合正则表达式和URL的全局变量是预定义的,用于获取请求、连接和服务器等信息
Rewrite实际场景
rewrite放在server{},if{},location{},段中
location只对域名后面除去传递参数外的字符串起作用
对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理
Nginx正则表达式
常用的正则表达式元字符
Rewrite语法
rewrite[flag //rewrite支持的flag标记];
flag标记说明
last和break比较
location分类
分类
location=patt{}[精准匹配]
location patt {}[一般匹配]
location ~patt {}[正则匹配]
正则匹配的常用表达式
location优先级
相同类型的表达式,字符串长的会优先匹配
按优先级排序
=类型
^~类型表达式
正则表达式(~和~*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其他匹配,任何请求都会匹配到
比较rewrite和location
相同点
都能实现跳转
不同点
rewrite是在同一域名内更改获取资源的路径
location是对一类路径做控制访问或反向代理还可以proxy_pass到其他机器
rewrite会写在location里面,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
location优先级规则
匹配某个具体文件
(location=完整路径)>(location^~完整路径)>(location~*完整路径)>(location~完整路径)>(location 完整路径)>(location /)
用目录做匹配访问某个文件
(location=目录)>(location^~目录)>(location~目录)>(location~*目录>(location 目录)>(location /)
代码操作
#使用yum安装nginx
[root@bogon ~]# vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@bogon ~]# yum clean all
[root@bogon ~]# yum makecache
[root@bogon ~]# yum -y install nginx
[root@bogon ~]# nginx -v #查看nginx版本号
[root@bogon ~]# systemctl enable nginx
[root@bogon ~]# systemctl start nginx
[root@bogon ~]# ls /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
[root@bogon ~]# cd /etc/nginx/conf.d/
[root@bogon conf.d]# ls
default.conf
[root@bogon conf.d]# vi kc65.conf
server {
listen 80;
server_name www.kc65.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@bogon conf.d]# systemctl reload nginx
然后打开一台桌面版centos7 配置hosts解析然后在浏览器访问
[root@bogon conf.d]# vi newkc65.conf
server {
listen 80;
server_name www.newkc65.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@bogon conf.d]# systemctl reload nginx
在centos7上面配置hosts解析然后访问
[root@bogon conf.d]# vi kc65.conf
server {
listen 80;
server_name www.kc65.com;
rewrite ^/(.*)$ http://www.newkc65.com/$1 permanent;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@bogon conf.d]# nginx -s reload
[root@bogon conf.d]# vi /usr/share/nginx/html/weihu.html
<html>
<head>
<title>Test</title>
<meta charset="utf-8"></meta>
</head>
<body>
<p>本网站正在维护,维护时间大概两小时</p>
</body>
</html>
[root@bogon conf.d]# vi newkc65.conf
server {
listen 80;
server_name www.newkc65.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /weihu.html {
root /usr/share/nginx/html;
}
}
[root@bogon conf.d]# systemctl reload nginx
然后去桌面版centos7浏览器访问http://www.newkc65.com/weihu.html
[root@bogon conf.d]# vi newkc65.conf
server {
listen 80;
server_name www.newkc65.com;
set $rewrite true;
if ($remote_addr = '192.168.93.138'){
set $rewrite false;
}
if ($rewrite = true) {
rewrite (.+) /weihu.html;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /weihu.html {
root /usr/share/nginx/html;
}
}
#这里面设置的IP是指定的测试IP只有这个IP才能看见Nginx欢迎界面其他的IP只能看见维护界面
[root@bogon conf.d]# nginx -s reload
注意事项
重写规则是按照它们在配置文件中的顺序执行的,所以你应该将更具体的规则放在前面
尽量避免在 location
块中使用复杂的重写逻辑,因为这可能会降低 Nginx 的性能
在使用 last
和 break
标志时要小心,因为它们会改变 Nginx 处理请求的流程
使用 redirect
和 permanent
标志时,要确保你真正希望执行重定向,因为这会导致浏览器更新其书签和历史记录