Loading

Nginx配置正向代理

最近一个支付项目,需要访问外部支付接口,所以使用Nginx做一个正向代理,让其使用。
记录过程如下:

新增一个代理server


server {
    resolver 114.114.114.114;       #指定DNS服务器IP地址 
    listen 192.168.0.28:10080;
    location / {
        proxy_pass http://$host;     #设定代理服务器的协议和地址 
        proxy_set_header HOST $host;
        proxy_buffers 256 4k;
        proxy_max_temp_file_size 0k;
        proxy_connect_timeout 30;
        proxy_send_timeout 60;
        proxy_read_timeout 60;
        proxy_next_upstream error timeout invalid_header http_502;
    }
}

重启服务后,在另一台服务器上测试是否可用

[root@appserver33 APP]# curl -I http://www.qq.com -x 192.168.0.28:10080
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.20.1
Date: Fri, 17 Dec 2021 10:19:19 GMT
Content-Type: text/html
Content-Length: 151
Connection: keep-alive
Location: https://www.qq.com/

# 通过全局设置代理
[root@appserver33 APP]# export http_proxy='192.168.0.28:10080'
[root@appserver33 APP]# curl -I http://www.qq.com
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.20.1
Date: Fri, 17 Dec 2021 10:22:12 GMT
Content-Type: text/html
Content-Length: 151
Connection: keep-alive
Location: https://www.qq.com/

通过代理访问http协议以实现。

发现问题,无法访问https协议

[root@appserver33 APP]# curl -I https://www.qq.com -x 192.168.0.28:10080
HTTP/1.1 400 Bad Request
Server: nginx/1.20.1
Date: Fri, 17 Dec 2021 10:26:41 GMT
Content-Type: text/html
Content-Length: 157
Connection: close

curl: (56) Received HTTP code 400 from proxy after CONNECT

通过查找资料发现: 默认nginx没有加载https的代理模块,通过打补丁的方式,然后编译安装就可以。

https://github.com/chobits/ngx_http_proxy_connect_module

我这里因为只需要代理http协议,就没有往下走了。

传统的正向代理软件还有: squid

posted @ 2021-12-17 18:16  头痛不头痛  阅读(2571)  评论(0编辑  收藏  举报