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 @   头痛不头痛  阅读(2613)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩