location匹配的优先级和重定向

nginx的重定向(rewrite)

location 匹配

location匹配的就是后面的uri

/wordpress 192.168.233.10/wordpress

location匹配的分类和优先级

1.精确匹配

location = / 对字符串进行完全匹配,必须完全符合

2.正则匹配

^-前缀级别,以什么为开头

~区分大小写的匹配

~*不区分大小写

!~:区分大小写的取反

!~*:不区分大小写的取反

3.一般匹配(通用匹配)

location /字符串

精确匹配的优先级最高,其次是正则,最后是一般。

优先级总结:

location = 完整路径 > location ^~ >location ~ location~* >location /部分起始位置 > location /

实际网站中的所有规则:

第一种:网站首页

bash 复制代码
location = / {
	root html;
	index index.heml index.htm index.php;
}

第二章:处理静态请求的页面

bash 复制代码
location ^~ /static/ {
	root /web/static/;
	index index.heml index.htm;
}

访问图片或者是指定的后缀名

bash 复制代码
location ~* \\.(jpg|gif|png|css) {
	root /web/pictures/;
	index index.heml index.htm;
}

第三章:一般是通用规则,原来转发.php .js为后缀的动态请求到后台服务器(数据库)。

转发后端请求和负载均衡

location / {

​ proxy_pass

}

rewrite重定向:

rewrite就是把当前访问的页面跳转到其他页面。

rewrite的工作方式:通过nginx的全局变量或者自定义变量,结合正则表达式和标志位实现url的重定向

nginx的变量

$uri 客户端请求的uri的地址

$host 请求的主机名

$http_user_angent 客户端请求的浏览器和操作系统

$http_referer 请求头的referer信息,表示当前页面来源的url

$remote_addr 客户端的IP地址

$remote_port 客户端的端口号

$server_addr 服务端的IP地址

$server_port 服务端的端口号

$request_method 获取客户端请求的方法

$scheme 显示请求的协议,要么是http要么是https

x_forwarded_for 用来获取请求头当中客户端的真实IP地址。代理服务器添加,在代理服务器当中指示客户端的IP地址

X-Real-IP:客户端真实的IP地址

vim nginx.conf

proxy_set_header X-Real-IP $remote_addr

加上这一字段,客户端的真实IP地址就会传递给后端服务器

标志位

flag

permanent:永久重定向,返回码是301,浏览器地址栏会显示跳转后的URL地址

redirect:临时重定向,返回码是302,浏览器地址栏会显示跳转后的URL地址

break:永久重定向,返回码也是301,但是它匹配到规则之后,不会在向下匹配其他规则,URL也不会发生变化

last:重定向,但是会继续向下匹配其他的location规则

rewrite的执行顺序:

1.server模块的rewrite优先级最高

2.匹配location的规则

3.执行选定的location规则

rewrite的语法:

rewrite 正则表达式 跳转后的内容 标志位;

在重定向的过程中,使用了last方式进行重定向,但是,没有结束语,陷入了死循环,nginx会自动循环10次,last最多只能循环10次,超过10次nginx就会自动结束,返回码500。

基于域名进行跳转,老的不用,但是依然能够访问,统统跳转到新的域名

bash 复制代码
vim /usr/local/nginx/conf/nginx.conf
server {
	listen 80;
	server_name www.xy102.com;
	charset utf-8;
	location / {
	root html;
	if ( $host = 'www.xy102.com'){
	rewrite ^/(.*)$ http://www.cj.com/$1 permanent;
	}
	index index.html;
	}
}
nginx -t 
systemctl restart nginx
#进入/etc/hosts做映射
vim /etc/hosts
192.168.233.10 www.xy102.com www.cj.com
#在index.html中添加内容
echo "" > /usr/local/nginx/html/index.html

基于客户端的ip进行跳转,公司新业务上线,测试阶段,

bash 复制代码
vim nginx.conf
set $rewrite true;
#设置应该变量名,rewrite,值是true
#来判断ip是否为合法ip
if ( $remote_addr = "192.168.233.10") {
	set $rewrite false;
}
if ( $rewrite = true ) {
	rewrite (.+) /error.html;
	#重定向,192.168.233.22/error.html
}
location = /error.html {
	root html;
}

location匹配的优先级

8.233.10") {

set $rewrite false;

}

if ( $rewrite = true ) {

rewrite (.+) /error.html;

#重定向,192.168.233.22/error.html

}

location = /error.html {

root html;

}

相关推荐
汤姆yu5 小时前
Gemini 4 Argon模型综述:技术、能力与实际使用指南
网络·安全·web安全·ai·大模型
hasty6 小时前
policy.xml 在,不代表策略生效:ImageMagick 补丁给安全配置验收的提醒
xml·mysql·安全
Seraphina366 小时前
SQL注入(二)盲注
网络·数据库·经验分享·笔记·sql
2401_868534787 小时前
无线网络规划设计
服务器·网络
螺蛳粉 螺蛳粉7 小时前
第二篇:keepalived VRRP 原理篇:协议与 VIP 漂移机制深入解析
网络·智能路由器·负载均衡·keepalived·高可用
寻林写彡7 小时前
云安全 | Docker 容器逃逸复盘(一):从隔离边界到运行时链路,如何确认自己身处容器
安全·云原生
盟接之桥7 小时前
AI助手:你的7×24小时智能管家——让异常预警无处不在
大数据·网络·数据库·人工智能·制造·ai编程
网络毒刘9 小时前
Manual 模式精修补丁:在 Agent 提案后用最小编辑完成高风险改动
安全·agent·ai编程·cursor
2603_969579289 小时前
内网数据自动备份工具:隔离网络下的数据保护方案
网络
逗脑IDE9 小时前
零基础学ESP32:RFID无线射频卡——让ESP32拥有“刷卡“能力!
开发语言·网络·人工智能·python·esp32·硬件开发