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:
1 location /proxy2 {
2 default_type text/html;
3
4 set $flag 0;
5
6 set_by_lua $flag '
7 local args = ngx.req.get_uri_args()
8 if args["a"] == "1" then
9 return 1
10 end
11 return 0
12 ';
13
14 if ($flag ~ 0) {
15 return 0;
16 }
17
18 proxy_pass http://127.0.0.1/example.php;
19
20 proxy_redirect off;
21
22 proxy_set_header Host $host;
23 proxy_set_header X-Real-IP $remote_addr;
24 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
25 proxy_max_temp_file_size 0;
26 proxy_connect_timeout 90;
27 proxy_send_timeout 90;
28 proxy_read_timeout 90;
29 proxy_buffer_size 4k;
30 proxy_buffers 4 32k;
31 proxy_busy_buffers_size 64k;
32 proxy_temp_file_write_size 64k;
33 }
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参数代理,原因相当费解也无从查明,最后决定绕过这个坎实现我的需求。
1 location /proxy {
2 set $url '';
3 set_by_lua $args "
4 local request_uri = ngx.var.request_uri
5 local poi = 0
6 for i = 1, #request_uri, 1 do
7 if string.sub(request_uri, i, i) == '?' then
8 poi = i
9 break
10 end
11 end
12 return string.sub(request_uri, poi)
13 ";
14
15 proxy_pass http://$url$args;
16
17 proxy_redirect off;
18 proxy_set_header Host $host;
19 proxy_set_header X-Real-IP $remote_addr;
20 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
21 proxy_max_temp_file_size 0;
22 proxy_connect_timeout 90;
23 proxy_send_timeout 90;
24 proxy_read_timeout 90;
25 proxy_buffer_size 4k;
26 proxy_buffers 4 32k;
27 proxy_busy_buffers_size 64k;
28 proxy_temp_file_write_size 64k;
29 }
相关文档:
https://github.com/chaoslawful/lua-nginx-module (nginx的lua库文档)
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com