使用nginx作为http/https正向代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 默认nginx是不支持https的正向代理的,要想通过nginx的正向代理,作为https服务器,需要增加一个模块并重新编译nginx。 环境: centos 7 所需要的文件: https: / / github.com / chobits / ngx_http_proxy_connect_module http: / / nginx.org / packages / centos / 7 / SRPMS / nginx - 1.12 . 2 - 1.el7_4 .ngx.src.rpm 安装编译工具 yum install gcc gcc - c + + make - y yum install rpm - build rpmdevtools - y 安装依赖 yum install pcre - devel pcre - y yum install zlib - devel zlib - y yum install openssl - devel openssl - y yum install redhat - lsb - core - y 下载nginx源码、模块源码及rpm构建包 cd / root # 用于支持https代理的模块 git clone https: / / github.com / chobits / ngx_http_proxy_connect_module.git # rpm构建及nginx源码 wget http: / / nginx.org / packages / centos / 7 / SRPMS / nginx - 1.12 . 2 - 1.el7_4 .ngx.src.rpm 修改nginx的src rpm包,增加ngx_http_proxy_connect_module模块 # 初始化rpmbuild目录 cd / root rpmdev - setuptree cp / root / nginx - 1.12 . 2 - 1.el7_4 .ngx.src.rpm / root / rpmbuild / SOURCES / cd / root / rpmbuild / SOURCES / rpm2cpio nginx - 1.12 . 2 - 1.el7_4 .ngx.src.rpm |cpio - dvi rm / root / rpmbuild / SOURCES / nginx - 1.12 . 2 - 1.el7_4 .ngx.src.rpm tar - xf nginx - 1.12 . 2.tar .gz cd / root / rpmbuild / SOURCES / nginx - 1.12 . 2 # 针对不同nginx版本,需要用不同的path文件,详见该项目github首页 patch - p1 < / root / ngx_http_proxy_connect_module / patch / proxy_connect_rewrite.patch cd cd / root / rpmbuild / SOURCES / tar - czvf nginx - 1.12 . 2.tar .gz nginx - 1.12 . 2 修改nginx.spec文件,增加模块的编译选项 将nginx.spec中的configure命令中,增加 - - add - module = / root / ngx_http_proxy_connect_module选项。 修改后的命令: % build . / configure % {BASE_CONFIGURE_ARGS} \ - - add - module = / root / ngx_http_proxy_connect_module \ - - with - cc - opt = "%{WITH_CC_OPT}" \ - - with - ld - opt = "%{WITH_LD_OPT}" \ - - with - debug make % {?_smp_mflags} % {__mv} % {bdir} / objs / nginx \ % {bdir} / objs / nginx - debug . / configure % {BASE_CONFIGURE_ARGS} \ - - add - module = / root / ngx_http_proxy_connect_module \ - - with - cc - opt = "%{WITH_CC_OPT}" \ - - with - ld - opt = "%{WITH_LD_OPT}" make % {?_smp_mflags} 编译rpm包 rpmbuild - bb nginx.spec rpm包,在 / root / rpmbuild / RPMS路径下。 修改nginx配置文件 基本的http代理,配置文件 / etc / nginx / conf.d / proxy.conf如下。为了适配https,我们需要增加connect语句相关的配置。 简单的http代理,配置文件 server { resolver 114.114 . 114.114 ; listen 9999 ; access_log / var / log / nginx / http_proxy.access.log main; error_log / var / log / nginx / http_proxy.error.log; location / { proxy_pass $scheme: / / $http_host$request_uri; } } https / http代理配置文件 server { resolver 114.114 . 114.114 ; listen 9999 ; proxy_connect; proxy_connect_allow 443 563 ; proxy_connect_connect_timeout 10s ; proxy_connect_read_timeout 10s ; proxy_connect_send_timeout 10s ; access_log / var / log / nginx / http_proxy.access.log main; error_log / var / log / nginx / http_proxy.error.log; location / { proxy_pass $scheme: / / $http_host$request_uri; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)