nginx lua 配合使用时的注意事项(汇总)
2015-04-10 00:39
337 查看
最近项目中使用到了nginx + lua 扩展方式来访问数据库。 其他服务向其发送HTTP请求获取数据。
在处理header 时有了一些问题,纠结了一阵子,最终解决了。
nginx 默认对 HTTP 协议的请求 header 进行合格检查,比如,header不能有下划线等,所以如果你要携带自定义的header 头,其格式写法最好和内置的保持一致,即最好用短横线 - 代替 下划线 _ ,
如果非要用下划线,那么需要配置 指令(http 或server下):
ignore_invalid_headers off; 是否忽略掉不合格的header域 默认是,
underscores_in_headers on; 是否允许 header 域 使用下划线
如果要在lua中 进行输出返回包的 header 域时, 若要输出带下划线的域,可以配置指令(在 http下)
lua_transform_underscores_in_response_headers off; 默认是开启的,那么nginx lua 会将 下划线转换为 短横; 配置关闭后,则不自动做转换。
如果觉得nginx默认的日志 时间戳很难看,那么可以在 配置文件中指定如下:
env TZ=UTC;
那么 时间格式 将变为 如 2015/02/10 14:05:21 , 即iso格式。
如果nginx lua 返回的包头,没有包含 Content-length 字段,那么在字段中可以显式指定下,输出中就会生效。
关于nginx header 下划线处理指令,详细介绍如下
syntax: lua_transform_underscores_in_response_headers on|off
default: lua_transform_underscores_in_response_headers on
context: http, server, location, location-if
Controls whether to transform underscores (
in the ngx.header.HEADER API to hypens (
This directive was first introduced in the
Enables or disables the use of underscores in client request header fields. When the use of underscores is disabled, request header fields whose names contain underscores are marked as invalid and become subject to the ignore_invalid_headers directive.
If the directive is specified on the server level,
its value is only used if a server is a default one. The value specified also applies to all virtual servers listening on the same address and port.
Controls whether header fields with invalid names should be ignored. Valid names are composed of English letters, digits, hyphens, and possibly underscores (as controlled by the underscores_in_headers directive).
If the directive is specified on the server level, its value is only used if a server is a default
one. The value specified also applies to all virtual servers listening on the same address and port.
在处理header 时有了一些问题,纠结了一阵子,最终解决了。
nginx 默认对 HTTP 协议的请求 header 进行合格检查,比如,header不能有下划线等,所以如果你要携带自定义的header 头,其格式写法最好和内置的保持一致,即最好用短横线 - 代替 下划线 _ ,
如果非要用下划线,那么需要配置 指令(http 或server下):
ignore_invalid_headers off; 是否忽略掉不合格的header域 默认是,
underscores_in_headers on; 是否允许 header 域 使用下划线
如果要在lua中 进行输出返回包的 header 域时, 若要输出带下划线的域,可以配置指令(在 http下)
lua_transform_underscores_in_response_headers off; 默认是开启的,那么nginx lua 会将 下划线转换为 短横; 配置关闭后,则不自动做转换。
如果觉得nginx默认的日志 时间戳很难看,那么可以在 配置文件中指定如下:
env TZ=UTC;
那么 时间格式 将变为 如 2015/02/10 14:05:21 , 即iso格式。
如果nginx lua 返回的包头,没有包含 Content-length 字段,那么在字段中可以显式指定下,输出中就会生效。
local ngx_return=function(data,code,err) --ngx.status=code local ret={} ret.data=data ret.code=code ret.err=err local jrt=cjson.encode(ret) ngx.header["Server"]=nil ngx.header["Date"]=nil --ngx.header["Connection"]= nil -- ngx.header["Connection"]="Keep-Alive" ngx.header["correlation_id"]=ngx.req.get_headers()["correlation_id"] <strong> ngx.header["Content-Length"] = string.len(jrt)</strong> ngx.say(jrt) ngx.flush(true) ngx.log(ngx.INFO,"return client ret=",jrt) --to cause quit the whole request rather than the current phase handler return ngx.exit(ngx.HTTP_OK) end
关于nginx header 下划线处理指令,详细介绍如下
lua_transform_underscores_in_response_headers
syntax: lua_transform_underscores_in_response_headers on|offdefault: lua_transform_underscores_in_response_headers on
context: http, server, location, location-if
Controls whether to transform underscores (
_) in the response header names specified
in the ngx.header.HEADER API to hypens (
-).
This directive was first introduced in the
v0.5.0rc32release.
underscores_in_headers
Syntax: | underscores_in_headers on| off; |
---|---|
Default: | underscores_in_headers off; |
Context: | http, server |
If the directive is specified on the server level,
its value is only used if a server is a default one. The value specified also applies to all virtual servers listening on the same address and port.
ignore_invalid_headers
Syntax: | ignore_invalid_headers on| off; |
---|---|
Default: | ignore_invalid_headers on; |
Context: | http, server |
If the directive is specified on the server level, its value is only used if a server is a default
one. The value specified also applies to all virtual servers listening on the same address and port.
相关文章推荐
- 使用lua虚拟机的一个注意事项
- 安卓高手之路之图形系统【5】安卓ListView和EditText配合使用时的注意事项。
- 安卓高手之路之图形系统【5】安卓ListView和EditText配合使用时的注意事项。
- PHP使用Redis+Lua脚本操作的注意事项
- php与memcached配合使用注意事项
- cocos2d-lua:小技巧及注意事项汇总(不定时更新)
- MongoDB日常使用的技巧与注意事项汇总
- titanium mobile httpClient 在ios下的使用,后端应用服务器为nginx时注意事项
- Cocos2dx使用luaJavaBridge注意事项
- lucene3.x_基础使用及注意事项汇总[更新中]
- 在Linux/Unix中使用错误保留符需注意的事项(2006-02-17)
- ruby中字符串的转码 使用方法以及注意事项
- C++构造函数中使用new时应注意的事项!
- 在asp.net 2.0中使用CuteEditor 5.0的一个注意事项
- 使用CommandBuilder为DataAdaper生成的Command更新数据源时的注意事项!
- [小结]select的使用注意事项
- 使用UltraWebGrid控件分页功能注意事项
- C#使用CDO发送邮件 注意事项!!!
- 使用Ajax技术获得服务端信息时的注意事项
- 使用Matcher.appendReplacement的注意事项