/ 中存储网

openresty-Nginx+lua+redis反向代理应用服务器

2014-01-16 09:07:01 来源:IT技术网

1.安装配置

http://openresty.org/ 上这个网址下载并安装。

2.nginx反向代理应用服务器

问题2.1:什么是应用服务器?

实际上就是让nginx拥有处理业务的能力。

例如:mysql, redis 读写数据;发送socket请求;对http请求的筛选处理或者是业务层的用户验证等,这里不一一举例了。

问题2.2:为什么要选择openresty?

openresty是由nginx+lua结合的框架,据称可达到10k+的并发能力,这里我并没有实际验证过。但作为并发处理能力最优的nginx和简单高效的lua,再搭配上redis;因此不需要质疑他的处理能力,相信没有更好的组合了。

3.固定URL反向代理

Example:

 location /proxy2 {

default_type text/html;

set $flag ;

set_by_lua $flag '

local args = ngx.req.get_uri_args()

if args["a"] == "" then

return

end

return

';

if ($flag ~ ) {

return ;

}

proxy_pass http://127.0.0.1/example.php;

proxy_redirect off;

proxy_set_header   Host             $host;

proxy_set_header   X-Real-IP        $remote_addr;

proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_max_temp_file_size ;

proxy_connect_timeout      ;

proxy_send_timeout         ;

proxy_read_timeout         ;

proxy_buffer_size          4k;

proxy_buffers               32k;

proxy_busy_buffers_size    64k;

proxy_temp_file_write_size 64k;

 }

4.变量URL反向代理 

问题4.1:capture method.http_post 不发送代理post参数.

根据文档的描述

Issuing a POST subrequest, for example, can be done as follows

res = ngx.location.capture(

'/foo/bar',

{ method = ngx.HTTP_POST, body = 'hello, world' }

)

See HTTP method constants methods other than POST. The method option is ngx.HTTP_GET by default.

此上的请求仅仅是发送了body的内容, post args并没有发送出去,此时十分费解难道这个还不支持post的请求参数代理。

When the body option is not specified, the POST and PUT subrequests will inherit the request bodies of the parent request (if any).

去掉body也依旧无效,并且访问一直处于running的状态,并且的连接超时后打印结果卡在ngx.location.capture;难道是没有读取到post的内容一直轮询,经过一番调试后,关键的一行代码出现了。

-- Reads the client request body synchronously without blocking the Nginx event loop.

ngx.req.read_body()

原来openresty并不会在capture的时候自动判断body是否已经存在或再次读取body内容,而且一直阻塞访问,直到超时。所以在capture前先读取body内容就解决了前面的所有疑惑。

Example:

ngx.req.read_body()

res = ngx.location.capture('/proxy', {method = ngx.HTTP_POST, args = args, vars = {url = url}})

问题4.2:变量URL 反向代理比较特殊,经过验证nginx是不支持get参数代理.

很遗憾nginx不支持变量URL的get参数代理,原因相当费解也无从查明,最后决定绕过这个坎实现我的需求。

 location /proxy {

set $url '';

set_by_lua $args "

local request_uri = ngx.var.request_uri

local poi =

for i = , #request_uri,  do

if string.sub(request_uri, i, i) == '?' then

poi = i

break

end

end

return string.sub(request_uri, poi)

";

proxy_pass http://$url$args;

proxy_redirect off;

proxy_set_header   Host             $host;

proxy_set_header   X-Real-IP        $remote_addr;

proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_max_temp_file_size ;

proxy_connect_timeout      ;

proxy_send_timeout         ;

proxy_read_timeout         ;

proxy_buffer_size          4k;

proxy_buffers               32k;

proxy_busy_buffers_size    64k;

proxy_temp_file_write_size 64k;

 }

相关文档:

https://github.com/chaoslawful/lua-nginx-module (nginx的lua库文档)