正向代理访问https;报错 curl: (56) Received HTTP code 502 from proxy after CONNECT NGINX报错:proxy_connect: connection error while connecting to upstream
内网服务器通过正向代理nginx,访问公网业务平台。
文章目录
前言
内网服务器通过nginx正向代理功能实现访问公网第三方系统对接业务。
一、正向代理配置
可参考:https://www.jb51.net/server/2915732k2.htm
注: nginx安装完毕后,仅可代理转发http流量;
可利用git开源模块https://github.com/chobits/ngx_http_proxy_connect_module 实现https流量代理转发。
在编译正向代理模块"patch -p1 < /**"的时候需要进入nginx解压目录下,“cd nginx-1.x.x/”,一定要带“/”,否则编译可能会报错:“error”。
二、测试正向代理
- curl -I http://www.baidu.com/ -v -x 127.0.0.1:80
curl -I https://www.baidu.com/ -v -x 127.0.0.1:443
注:80一般会正常返回200值。443报错。原因可能有conf/nginx.conf https的配置有问题,或者模块未编译成功。
此处为我的https配置,修改https默认监听端口为6363(不修改默认443)。此时我的代理地址就为:“本机ip:6363”
server {
#指定DNS服务器IP地址
resolver 10.51.0.11;
#监听6363端口,https默认端口6363
listen 6363;
##正向代理转发https请求
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 1000;
proxy_connect_read_timeout 1000;
proxy_connect_send_timeout 1000;
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
配置放在 http{ 后;
2.配置完毕,curl https://www.ctrip.com,成功返回200请求值及内容。
三、多次请求异常报错
curl: (56) Received HTTP code 502 from proxy after CONNECt;NIGNX报错:proxy_connect: connection error while connecting to upstream
尝试过多种方式,优化nginx-proxy,包括超时、size均无效。
后在DNS配置后添加“ipv6=off”。解决。
四、 总结
正向代理出现标题报错内容,可在监听端口后关闭IPV6访问解决。