使用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 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?