nginx 本身只支持 http 的正向代理,如果需要要配置 https 正向代理,需要为 nginx 安装三方模块:ngx_http_proxy_connect_module, 这部分操作我们需要重新编译 nginx 才能完成, 下面将详细说明步骤
安装 nginx
准备文件目录
我们准备一个目录将所有相关的文件都放在该目录下, 例如:
mkdir -p /opt/nginx && cd /opt/nginx
下载 nginx 安装包
下载地址: https://nginx.org/en/download.html
这里我们选择1.18版本
wget https://nginx.org/download/nginx-1.18.0.tar.gz
下载 ngx_http_proxy_connect_module 模块
下载地址: https://github.com/chobits/ngx_http_proxy_connect_module/releases
wget https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v0.0.5.tar.gz
安装
解压所有文件
tar -zxvf nginx-1.18.0.tar.gz
tar -zxvf ngx_http_proxy_connect_module-0.0.5.tar.gz
mv ngx_http_proxy_connect_module-0.0.5 ngx_http_proxy_connect_module # 重命名一下文件夹名
添加模块
cd nginx-1.18.0.tar.gz
patch -p1 < /opt/nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
这里要注意, 这个模块是和 nginx 的版本有关系的, 我们需要根据 nginx 版本来选择特定模块
配置安装
./configure --prefix=/usr/local/nginx --add-module=/opt/nginx/ngx_http_proxy_connect_module
这一步可能会提示你缺少依赖, 可以根据提示进行安装依赖
apt install libpcre3 libpcre3-dev
apt install libssl-dev
apt install libxml2-dev libxslt1-dev
可以参考: https://blog.csdn.net/weixin_45729432/article/details/129493752
安装
make && make install
添加软链接
我们把 nginx 安装在了 /usr/local/nginx (默认也是这个位置), 我们可以添加软链接, 使得访问更方便
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
配置
可以参考组件的说明文档
server {
listen 3128;
# dns resolver used by forward proxying
resolver 8.8.8.8;
# forward proxy for CONNECT requests
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_data_timeout 10s;
# defined by yourself for non-CONNECT requests
# Example: reverse proxy for non-CONNECT requests
location / {
proxy_pass http://$host;
proxy_set_header Host $host;
}
}
注意点
如果在手动安装 nginx 之前, 装有其他版本的 nginx, 那么则会遇到 nginx: [emerg] unknown directive "proxy_connect"
问题, 所以在安装前务必删除掉旧的 nginx, 使用 whereis nginx
列出相关的文件, 清理掉即可
参考: https://github.com/chobits/ngx_http_proxy_connect_module/issues/236