目录
- 背景
- 问题
- 原因和解决方法
- 总结
背景
netstub项目中,使用openresty作为网关,proxy_pass处理。
配置如下:
access.lua中进行一些预处理,然后代理到目标服务
…
location / {
access_by_lua_file /etc/nginx/conf.d/access.lua;
resolver 8.8.8.8;
proxy_pass http://$http_host;
proxy_buffering off;
}
…
问题
请求过程中发现,使用了nginx代理之后,请求变慢非常多,每个请求都将近1s。
本地启动netstub调试,直接请求http://httpbin.org/get?biz=test, 响应时间在200多ms
使用反向代理,请求http://127.0.0.1/get?biz=test, 响应时间达到了900多ms
原因和解决方法
几经辗转发现是http协议版本导致的,代理之后走的是http 1.0。
导致请求变慢。
在nginx配置文件中增加proxy 版本配置proxy_http_version 1.1; 指定为1.1版本即可
server {
…
proxy_http_version 1.1;
location / {
access_by_lua_file /etc/nginx/conf.d/access.lua;
resolver 8.8.8.8;
proxy_pass http://$http_host;
proxy_buffering off;
}
…
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。