phase: output-header-filter
WARNING Since the v0.9.17 release, use of this directive is discouraged; use the new header_filter_by_lua_block directive instead.
Uses Lua code specified in
to define an output header filter.
Note that the following API functions are currently disabled within this context:
Output API functions (e.g., ngx.say and ngx.send_headers)
Control API functions (e.g., ngx.redirect and ngx.exec)
Subrequest API functions (e.g., ngx.location.capture and ngx.location.capture_multi)
Cosocket API functions (e.g., ngx.socket.tcp and ngx.req.socket).
Here is an example of overriding a response header (or adding one if absent) in our Lua header filter:
location / {
proxy_pass http://mybackend;
header_filter_by_lua 'ngx.header.Foo = "blah"';
This directive was first introduced in the v0.2.1rc20 release.
官网:
https://github.com/openresty/lua-nginx-module#header_filter_by_lua
a)新增自定义的HTTP头
b)重新设置upstream返回的HTTP头,也就是起到覆盖作用
使用示范源码:nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
http {
log_format main '$msec $status $request $request_time '
'$http_referer $remote_addr [ $time_local ] '
'$upstream_response_time $host $bytes_sent '
'$request_length $upstream_addr';
access_log logs/access.log main buffer=32k flush=1s;
upstream remote_world {
server 127.0.0.1:8080;
server {
listen 80;
location /exec {
content_by_lua '
local cjson = require "cjson"
local headers = {
["Etag"] = "663b92165e216225df78fbbd47c9c5ba",
["Last-Modified"] = "Fri, 12 May 2016 18:53:33 GMT",
ngx.var.my_headers = cjson.encode(headers)
ngx.var.my_upstream = "remote_world"
ngx.var.my_uri = "/world"
ngx.exec("/upstream")
location /upstream {
internal;
set $my_headers $my_headers;
set $my_upstream $my_upstream;
set $my_uri $my_uri;
proxy_pass http://$my_upstream$my_uri;
header_filter_by_lua '
local cjson = require "cjson"
headers = cjson.decode(ngx.var.my_headers)
for k, v in pairs(headers) do
ngx.header[k] = v
server {
listen 8080;
location /world {
echo "hello world";
运行:openresty -p `pwd` -c conf/nginx.conf
运行结果:
原文出自:http://blog.csdn.net/daiyudong2020/article/details/53149242
header_filter_by_lua的说明:header_filter_by_luasyntax: header_filter_by_lua context: http, server, location, location ifphase: output-header-filterWARNING Since the v0.9.17 release, use of th
OpenResty 是一个通过扩展 nginx 的快速 Web 应用服务器。
Nginx Openresty For Windows (NOW) 是带有 Openresty 的 Windows 版本中的 Nginx。
它有一些特点:
并发两万多个连接
支持共享内存
支持udp代理
与 nginx 原始版本相比修复的各种错误
它已符合这些官方模块:
http_realip_module
http_addition_module
http_sub_module
http_dav_module
http_stub_status_module
http_flv_module
http_mp4_module
http_gunzip_module
http_gzip_static_module
http_auth_request_module
http_random_index_module
http_secure_link_module
http_ssl_module
mail_ssl_module
http_v2_module
and these addon module:
和这些插件模块:
array-var-nginx-module
ngx_devel_kit
ngx-coolkit-module
echo-nginx-module
form-input-nginx-module
encrypted-session-nginx-module
set-misc-nginx-module
ngx-postgres-module
ngx-lua-module
ngx_lua_upstream
headers-more-nginx-module
rds-json-nginx-module
nginx-http-concat-module
redis2-nginx-module
and these C module for Lua:
以及这些用于 Lua 的 C 模块:
lua-cjson
lua-redis-parser
lua-rds-parser
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis
Nginx与Lua编写脚本的基本构建块是指令。 指令用于指定何时运行用户Lua代码以及如何使用结果。 下面是显示指令执行顺序的图。
当一个请求发起一个“子请求”的时候,按照 Nginx 的术语,习惯把前者称为后者的“父请求”(parent request)。
location /main...
安装依赖包
yum install -y zlib-devel gcc-c++ pcre-devel openssl openssl-devel lua-devel
安装LuaJIT
wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -axv -f LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make && make install PREFIX=/etc/LuaJIT
九、控制响应头
HTTP响应头需要配置很多重要的信息,例如添加CDN缓存时间、操作set-cookie、标记业务数据类型等。利用Lua的API可以轻松完成这些配置,并且它有丰富的模块可供选择。
9.1 获取响应头
ngx.resp.get_headers语法:headers = ngx.resp.get_headers(max_headers?, raw?)配置环境:set_by_lua,rewr...
1、每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM;
2、将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问;
3、每个外部请求都由一个Lua协程处理,协程之间数据隔离;
4、Lua代码调用I/O操作等异步接口时,会挂起当前协程(并保护上下文数据),而不阻塞worker;
5、I/O等异步操作完成时还原相关协程上
ldzjack:
希尔排序详解
二哈喇子!:
IPC之Posix共享内存详解
起个名都这么难:
linux系统负载load average的含义
做人难难难啊啊啊啊: