nginx rewrite proxy_pass location 的理解
来源:互联网 发布:免费的gis软件 编辑:程序博客网 时间:2024/04/30 15:13
- rewrire
rewrite 模块简介
rewrite 是nginx的重写模块,是一个正则表达式与虚拟机的结合。因为loaction是遍历都执行一遍(没有匹配成功就 /);所以,
rewrite 到新的uri后进入另外一个location ,恰好这个location也有rewrite 再次重新的uri 又调回原来的location;这样就是
四循环了,最多执行10次,然后报500错误。
rewrite 模块指令
[html] view plain copy
set
if
return
last #rewrite 完成,但会进入location,再次rewrite
break #rewrite localtion 都完成,不会再进入localtion rewrite;但也不是用break就万事大吉了;有时必须用last
重写规则语法: rewrite 正则 替换 标志位
(1). 任何重写规则第一部分肯定是 正则表达式
可以用()来捕获正则表达式,然后在rewrite后面就用 $[d+] 来用这个正则表达式;比如:
[html] view plain copy 在CODE上查看代码片派生到我的代码片
^/images/([a-z]{2})/([a-z0-9]{5})/(.*).(png|jpg|gif)$ —->http://xxxx.com/images/aa/abc01/test.gif
其中
1=([a-z]{2}) #1=aa
2=([a-z0-9]{5}) #2=abc01
3=(.*) #3=test
4=(png|jpg|gif) #4=gif
上面的4个地段都是query 串中匹配的字符串
(2). 重写规则的第二部分是URI(rewrite 重写后url)
query 请求串被改写,包括上面正则表达式获取的参数字段和nginx 相关的配置,
[html] view plain copy 在CODE上查看代码片派生到我的代码片
/data?file=
rewrite 之后会返回给客户端301 或者302 [ 会不会返回200]
(3). 重写规则的第三部分是:尾部的标记 flag last return break
laster 标记之后会从新loaction ,继续rewrite 最多10次
[html] view plain copy 在CODE上查看代码片派生到我的代码片
rewrite ‘^/images/([a-z]{2})/([a-z0-9]{5})/(.*).(png|jpg|gif)
http://xxxx.com/images/aa/abc01/test.gif —–rewrite—> http://data?file=test.gif
break标记是直接跳槽rewrite和localtion 进行query的处理if ($bwhog) {
[html] view plain copy 在CODE上查看代码片派生到我的代码片
if ($bwhog) {
limit_rate 300k;
break;
}
return标记停止rewrite 处理指令,进而控制主HTTP 模块处理请求,也就是HTTP请求也不处理了,直接给client 返回(结合error0page,)
[html] view plain copy 在CODE上查看代码片派生到我的代码片
location = /image404.html {
return 404 “image not found\n”;
}
(4). 整个过程的实例
[html] view plain copy 在CODE上查看代码片派生到我的代码片
http {
server {
root /home/www;
location / { # 重写规则信息 error_log logs/rewrite.log notice; # 注意这里要用‘’单引号引起来,避免{} rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4; # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行 set $image_file $3; set $image_type $4; } location /data { rewrite /data?(.*) /error/img=$1 break; #break 就是最后的rewrite 结果,不会再次遍历localtion啦 } location = /error/ { # 图片不存在返回特定的信息 return 404 "image not found\n"; #如果是return HTTP 直接返回403 302 等状态码 }
}
2.location
根据正则表达式进行匹配query串, / 是所有的匹配中最低优先级,其他的不能匹配在到这里面处理;
如果其他的能精确匹配 就不会向下走,直接处理;
locaton 里面可以有proxy_pass 的命令和rewrite的命令
3.proxy_pass
把query 转发给指定的query串,这个query串是固定的,不能用正则表达式,也不能用老query串的参数;
rewrite 就可以用正则表达式,也可以用老query的串,所以相比 rewrite 更加灵活、强大
- nginx rewrite proxy_pass location 的理解
- nginx rewrite proxy_pass location 的理解
- nginx中location中关于proxy_pass和rewrite的应用
- nginx详细解读location rewrite和proxy_pass
- nginx rewrite proxy_pass location实践及正则匹配
- nginx中rewrite和proxy_pass
- nginx location节点 proxy_pass 中的”/”
- nginx的proxy_pass和 location为正则表达式的问题
- Nginx的location和rewrite配置语法
- nginx,rewrite,proxy_pass,post数据,表单
- nginx-location及rewrite
- location proxy_pass的学习资料
- nginx proxy_pass 里的”/”
- Nginx的proxy_pass配置
- Nginx location 和 rewrite retry
- nginx location rewrite匹配顺序
- [nginx] if、rewrite、location指令
- nginx--- location 和 rewrite 顺序
- soot代码分析框架的基础知识(一)
- Hadoop MapReduce 程序执行过程
- Android学习系列(18)--App数据格式之解析Json
- Android 使用Retrofit2问题汇总
- POJ 3278 Catch That Cow(DFS)
- nginx rewrite proxy_pass location 的理解
- 针对苹果最新审核要求 为应用兼容IPv6
- 欢迎使用CSDN-markdown编辑器
- Android 面试题总结之Android 基础(二)
- 设置行间距,计算label的高度 ios
- C++primer_第七章_类_学习跟踪
- 如何从频谱图看一副图像的高频和低频成分
- WVPlots: example plots in R using ggplot2
- mysql数据库开发规范