• Centos7使用Nginx配置HTTPS正向代理


    Centos7使用Nginx配置HTTPS正向代理

    Nginx默认安装只支持代理HTTP, 不支持代理 HTTPS, 如果需要支持HTTPS需要在Nginx编译时加入 ngx_http_proxy_connect_module.

    文件准备

    1. 从 ngx_http_proxy_connect_module 模块仓库下载源代码压缩包, 并确定支持的 Nginx 版本
      • 仓库地址: https://github.com/chobits/ngx_http_proxy_connect_module/
      • 得到文件: ngx_http_proxy_connect_module-master.zip
      • 当前支持的 Nginx 最高版本为 1.25.x
    2. 从 Nginx 官网下载对应版本的源代码压缩包
      • 下载列表: http://nginx.org/download/
      • 得到文件: nginx-1.25.5.tar.gz

    环境准备

    需要一台能访问目标网络的服务器, 并新安装 Centos7 环境

    更新各模块安装包

    sudo yum update
    
    • 1

    安装编译Nginx需要的工具

    sudo yum install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel patch unzip
    
    • 1

    编译 Nginx

    在同一目录下解压 Nginx 和 ngx_http_proxy_connect_module.

    tar -xzvf nginx-1.25.5.tar.gz
    unzip ngx_http_proxy_connect_module-master.zip
    
    • 1
    • 2

    应用补丁, Nginx 版本1.25.5 对应的补丁文件是 proxy_connect_rewrite_102101.patch, 如果是其它版本, 请参照 https://github.com/chobits/ngx_http_proxy_connect_module/ 上的说明确认对应的补丁文件

    cd nginx-1.25.5
    patch -p1 --verbose < ../ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch
    
    • 1
    • 2

    配置编译环境, 如果配置过程中报错, 需要排查并解决错误

    ./configure --add-module=../ngx_http_proxy_connect_module-master
    
    • 1

    编译

    make
    
    • 1

    安装

    sudo make install
    
    • 1

    Nginx 会被默认安装到 /usr/local/nginx 目录

    配置 Nginx

    编辑 /usr/local/nginx/conf/nginx.conf, 在 http { } 内增加一个新的 server 配置, 内容如下

    其中:

    • listen 3128; 是代理的端口, 可以换成其它端口
    • server_name 192.168.9.160; 是代理服务器自身IP, 需要换成你的环境中服务器的IP
    • resolver 114.114.114.114; 是代理服务器使用的DNS服务器, 需要换成你的环境下可用的DNS服务器地址
    • 其他配置不需要修改
        server {
            listen       3128;
            server_name  192.168.9.160;
            resolver     114.114.114.114;
    
            # forward proxy for CONNECT requests
            proxy_connect;
            proxy_connect_allow            443 563;
            proxy_connect_connect_timeout  10s;
            proxy_connect_data_timeout     10s;
    
            # Example: reverse proxy for non-CONNECT requests
            location / {
                proxy_pass http://$host;
                proxy_set_header Host $host;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    检查配置格式是否正确, 如果有错误信息, 需要先排查问题

    sudo /usr/local/nginx/sbin/nginx -t
    
    • 1

    启动Nginx服务

    sudo /usr/local/nginx/sbin/nginx
    
    • 1

    打开防火墙端口

    如果服务器开启了 firewalld 等防火墙服务, 需要打开上面设置的代理端口, 允许其它机器访问

    检验

    首先打开 Nginx 日志

    tail -f /usr/loca/nginx/logs/access.log
    
    • 1

    在另一台服务器上, 使用 curl 工具进行测试, 下面的命令中需要将192.168.9.160:3128换成自己环境下配置的代理服务器IP和端口

    curl https://twitter.com/ -v -x 192.168.9.160:3128
    
    • 1

    如果代理工作正常, 如果输出如下结果, 则表示配置成功

    * About to connect() to proxy 192.168.9.160 port 3128 (#0)
    *   Trying 192.168.9.160...
    * Connected to 192.168.9.160 (192.168.9.160) port 3128 (#0)
    * Establish HTTP proxy tunnel to twitter.com:443
    > CONNECT twitter.com:443 HTTP/1.1
    > Host: twitter.com:443
    > User-Agent: curl/7.29.0
    > Proxy-Connection: Keep-Alive
    >
    < HTTP/1.1 200 Connection Established
    < Proxy-agent: nginx
    <
    * Proxy replied OK to CONNECT request
    * Initializing NSS with certpath: sql:/etc/pki/nssdb
    *   CAfile: /etc/pki/tls/certs/ca-bundle.crt
      CApath: none
    * SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    * Server certificate:
    ...
    < HTTP/1.1 200 OK
    < Date: Wed, 13 Mar 2024 08:16:22 GMT
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    零基础自学Java的网站有哪些?
    docker-metabase镜像打入clickhouse驱动
    Leo赠书活动-01期 【Python数据挖掘——入门进阶与实用案例分析】
    【鸿蒙 HarmonyOS 4.0】TypeScript开发语言
    php+java+python学生日常行为管理系统
    软件系统功能测试的依据
    【数据挖掘】7. 多类别分类 多类别分类感知器(Perceptron)
    【Flocking算法】海王的鱼塘是怎样炼成的
    Hystrix原理
    《DevOps 精要:业务视角》- 读书笔记(六)
  • 原文地址:https://blog.csdn.net/michaelchain/article/details/138204662