nginx rewrite配置解读
序
本文主要解析一下ngx_http_rewrite_module中的rewrite相关配置。
directives
名称 |
默认配置 |
作用域 |
官方说明 |
中文解读 |
模块 |
---|---|---|---|---|---|
break |
无 |
server, location, if |
Stops processing the current set of ngx_http_rewrite_module directives. |
中断当前的重写 |
ngx_http_rewrite_module |
if |
无 |
server, location |
The specified condition is evaluated. If true, this module directives specified inside the braces are executed, and the request is assigned the configuration inside the if directive. Configurations inside the if directives are inherited from the previous configuration level. |
if表达式成立则执行,if代码块里头的配置继承外部的配置; false和0都表示不成立; string比较,相等用=,不相等用!= ; 文件存在用-f,不存在用! -f ; 目录存在用-d,不存在用 ! -d ; 链接存在用-e,不存在用! -e ; 文件可执行用-x,不可执行用! -x |
ngx_http_rewrite_module |
return |
无,语法 return code [text]; return code URL; return URL; |
server, location, if |
Stops processing and returns the specified code to a client. The non-standard code 444 closes a connection without sending a response header. |
停止处理并返回,返回444的话则会关闭连接,连header都不会发送 |
ngx_http_rewrite_module |
rewrite |
无,语法 rewrite regex replacement [flag]; |
server, location, if |
If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client. |
last停止匹配,使用重写后的url进行新的匹配; break停止当前的url重写; redirect进行302跳转; permanent进行301跳转 |
ngx_http_rewrite_module |
rewrite_log |
rewrite_log off; |
http, server, location, if |
Enables or disables logging of ngx_http_rewrite_module module directives processing results into the error_log at the notice level. |
设置是否在error_log以notice级别开启rewrite的log |
ngx_http_rewrite_module |
set |
无,语法 set $variable value; |
server, location, if |
Sets a value for the specified variable. The value can contain text, variables, and their combination. |
设置变量 |
ngx_http_rewrite_module |
uninitialized_variable_warn |
uninitialized_variable_warn on; |
http, server, location, if |
Controls whether warnings about uninitialized variables are logged. |
设置是否打印使用了未初始化变量的log |
ngx_http_rewrite_module |
实例
http {
# a special log format referencing variables we'll define later
log_format imagelog '[$time_local] ' $image_file '
'$image_type '
' $body_bytes_sent '
' $status;
# we want to enable rewrite-rule debugging to see if our rule does what we intend
rewrite_log on;
server {
root /home/www;
location / {
# we specify which logfile should receive the rewriteruledebug messages
error_log logs/rewrite.log notice;
# our rewrite rule, utilizing captures and positional variables
# note the quotes around the regular expression theseare required because we used {} within the expression itself
rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*).(png|jpg|gif)$' /data?file=$3.$4;
# note that we didn't use the 'last' parameter above; if we had, the variables below would not be set because NGINX would have ended rewrite module processing here we set the variables that are used in the custom log format 'imagelog'
set $image_file $3;
set $image_type $4;
}
}
}
doc
- ngx_http_rewrite_module
- 客户端无法连接数据库的小问题(r8笔记第53天)
- Golang事务模型
- 厚土Go学习笔记 | 35. web服务器实现动态路径
- 数据库连接池、dbutil_知识点全掌握
- Golang 序列化之 ProtoBuf
- Golang RPC 之 gRPC
- 解决连通性问题的四种算法
- 使用shell批量生成数据整合式迁移的脚本(r8笔记第52天)
- Jdbc知识点全整理,你值得拥有 (1)
- SSD: Single Shot MultiBox Detector 深度学习笔记之SSD物体检测模型
- dg broker校验失败的一个奇怪问题(r8笔记第50天)
- golang 几种字符串的连接方式
- 整理ING
- dg broker校验失败的一个奇怪问题(二) (r8笔记第51天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 优化循环的方法-循环展开
- 程序性能优化-局部性原理
- Codeforces Round #547 (Div. 3)E. Superhero Battle
- 《动态规划_入门 LIS 问题 》
- 栅格化系统的原理以及实现
- vue-qr二维码插件使用简介
- Codeforces Round #547 (Div. 3)F1. Same Sum Blocks (Easy)
- 手机软键盘弹起导致页面变形的一种解决方案
- Codeforces Round #547 (Div. 3)F2. Same Sum Blocks (Hard)
- 《 动态规划_ 入门_最大连续子序列 》
- docker搭建elasticsearch集群
- 用canvas实现手写签名功能
- Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland
- 《 动态规划_ 入门_最大连续子序列_HDU_1003 》
- 2015 09CCF计算机软件能力认证试题第三题