nginx支持lua的编译配置及Nginx rewrite对post数据的影响

星期四, 2016-12-08 | Author: Lee | JAVA-and-J2EE, linux | 4,589 views

nginx+lua 可以很方便做限流,路由等其他配置很是方便

编译配置如下:
lua-nginx-module 是 openresty(集成nginx版本) 下的一个模块可以独立编译挂载
https://github.com/openresty/lua-nginx-module

Alternatively, ngx_lua can be manually compiled into Nginx:

1.Install LuaJIT 2.0 or 2.1 (recommended) or Lua 5.1 (Lua 5.2 is not supported yet). LuaJIT can be downloaded from the LuaJIT project website and Lua 5.1, from the Lua project website. Some distribution package managers also distribute LuaJIT and/or Lua.

2.Download the latest version of the ngx_devel_kit (NDK) module HERE.

3.Download the latest version of ngx_lua HERE.

4.Download the latest version of Nginx HERE (See Nginx Compatibility)

下载编译安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
wget http://nginx.org/download/nginx-1.11.6.tar.gz
tar -xzvf nginx-1.11.6.tar.gz 
 
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar zxvf LuaJIT-2.0.4.tar.gz 
 
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar zxvf ngx_devel_kit-0.3.0.tar.gz 
 
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz
tar zxvf v0.10.7.tar.gz 
 
cd LuaJIT-2.0.4
make
make install
 
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
cd ../
cd nginx-1.11.6
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --add-module=../lua-nginx-module-0.10.7 --add-module=../ngx_devel_kit-0.3.0
make -j 4
make install
echo '/usr/local/lib' >> /etc/ld.so.conf.d/lua.conf
ldconfig
 
/usr/local/webserver/nginx/sbin/nginx -t
/usr/local/webserver/nginx/sbin/nginx -s reload

配置nginx支持lua
vi nginx.cnf

server 里添加

1
2
3
 location /lua {
            content_by_lua_file /home/lua/hello.lua;
        }

hello.lua脚本如下:

1
2
3
4
5
6
local ngx = ngx
 
ngx.header.content_type="text/plain";
ngx.say("hello IA!");
 
return ngx_ok

小计:本来是拿来做url地址重写的,发现不支持post的参数转发(同域名的下的可以),原因如下:
有个同事跟我咨询他的post请求,rewrite 之后,post的数据没有传过去。遂跟他要了下配置,示意如下:

1
2
3
4
5
6
{
servername i5a6.com
 location /abc {
 rewrite ^ http://i5a6.com/abc;
}
}

看到配置就不难理解了,原因是如果rewrite 后面的参数是以http开头,那么实际就是会redirect,给客户端返回临时重定向302,这时客户端会收到302后对foo.com发起get请求(通过firebug跟踪可见)。所以之前的post请求的数据就不复存在了。上面这种情况应该使用反向代理proxy_pass。

为了测试一下rewrite对post的影响,故设计了如下场景进行测试,以作备忘。
场景:
rewrite 进行外部redirect时,post数据会丢失,内部跳转呢。配置如下

1
2
3
4
5
6
7
8
9
{
servername bar.com
 location /abc {
 rewrite ^ /123;
}
 location /123 {
 proxy_pass http://192.168.0.100/myform   ;
}
}

(proxy_pass后端是用dancer起的一个处理post表单的程序)
bashcmd#curl -d “a=b” http://localhost/abc
测试结果显示后端的myform可以取到post参数,说明rewrite内部跳转时post数据可以传到后端。
分析了下原因应该是:
http协议中对request有如下描述,

1
2
3
4
5
6
7
Request = Request-Line ; Section 5.1
*(( general-header ; Section 4.5
| request-header ; Section 5.3
| entity-header ) CRLF) ; Section 7.1
CRLF
[ message-body ] ; Section 4.3
request_uri属于Request-Line。

post的数据存放在message_body内,在内部跳转时,nginx实际是把request uri重写了下,又放回request中,所以message_body的数据得以传到后端。

如果内部跳转使用第三方模块echo_location

1
2
3
location /abc {
 echo_location /123;
}

post数据也会丢失,因为echo_location实现机制是对重定向的地址进行GET操作。
post数据部分参考:http://www.xiehaichao.com/articles/428.html

Tags: ,

文章作者: Lee

本文地址: https://www.pomelolee.com/1643.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta