仅使用Nginx实现HTTPS正向代理

2023-12-5 159 12/5

Nginx 正向 HTTPS 代理,需要插件 ngx_http_proxy_connect_module 支持,且需要 auth_basic 作为访问认证,但由于 auth_basic 模块只能处理 Authorization header,所以还要加入 lua-nginx-module 来处理 Proxy-Authorization header 认证。简单点讲就是需要 nginx 加入 ngx_http_proxy_connect_module 和 lua-nginx-module 两个插件,来处理正向代理和认证。下面服务端以 Debian10 来示范一下编译、安装过程。开始之前,先安装一下依赖工具:apt update -yapt install -y build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev wget git这个插件,就是用来处理 HTTPS 正向代理的,项目地址:https://github.com/chobits/ngx_http_proxy_connect_module下载插件,以下俺未标记目录的,默认就是 root 目录:git clone https://github.com/chobits/ngx_http_proxy_connect_module.git需要先安装LuaJIT,并依赖ngx_devel_kit,如下:LuaJIT官网:http://luajit.org/安装完成后会显示:==== Successfully installed LuaJIT 2.0.5 to /usr/local/LuaJIT ====设置环境变量:vi /etc/profile添加变量:最后:source /etc/profile项目地址:https://github.com/simplresty/ngx_devel_kit回到 root 目录,下面下载的文件都在 root 目录:cd ..只需要下载就好了,不需要安装:git clone https://github.com/vision5/ngx_devel_kit.git项目地址:https://github.com/openresty/lua-nginx-module只需要下载就好了,不需要安装:git clone https://github.com/openresty/lua-nginx-module.git项目地址:https://github.com/openresty/lua-resty-core只需要下载就好了,稍后安装:git clone https://github.com/openresty/lua-resty-core.git项目地址:https://github.com/openresty/lua-resty-lrucache只需要下载就好了,稍后安装:git clone https://github.com/openresty/lua-resty-lrucache.git这次来下载 openresty 版的 Nginx 来编译:上面 nginx 就算编译完成了,再来安装一下 lua-resty-core 和 lua-resty-lrucache 依赖:最后把 path 引用加入 nginx 配置文件就好了,下面配置文件已添加:lua_package_path "/opt/nginx/lib/lua/?.lua;;";在写 nginx 配置文件之前,俺们先配置一下Basic Auth用户和密码。这个用户名和密码,后面连接代理的时候会用到。vi /etc/nginx/conf.d/proxy_auth.lua添加如下内容:vi /etc/nginx/nginx.conf首先还是要到 DNS 上添加一个 A 记录,这个就不赘述了。然后申请证书如下:安装certbot:apt install certbot -ycertbot申请证书:certbot certonly --standalone --email [email protected] -d xxxx.com运行完成后,会生成类似下面路径的证书,这个路径已经添加到nginx配置文件里面了,你只需要修改下域名:首先还是要写个 systemd 守护进程:vi /lib/systemd/system/nginx.service添加内容如下:重载服务:systemctl daemon-reload然后就可以启动Nginx了:systemctl start nginxNginx的常用命令systemctl enable nginx #设置开机自动启动systemctl status nginx #查看运行状态,显示running表示成功运行systemctl reload nginx #重新载入systemctl restart nginx #重新启动电脑端浏览器安装 SwitchyOmega 插件,新建一个代理情景模式,代理协议填写HTTPS,代理服务器填写你的域名,端口填写你使用的端口,如上 nginx 配置端口就填写3128,最后点击右边小锁,填写你的用户名(admin)和密码(12345)就可以使用了,如果需要分流的话,可以另建立一个自动切换模式。
仅使用Nginx实现HTTPS正向代理
在 SwitchyOmega 插件上设置代理自动分流模式,另新建一个自动切换模式,设置如下图:
仅使用Nginx实现HTTPS正向代理
AutoProxy:https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt移动端,俺们以小火箭和 Quantumult X 为例,小火箭,选择 HTTPS 代理,服务器填写你的域名,端口3128:
仅使用Nginx实现HTTPS正向代理
Quantumult X 配置文件/编辑/server_local字头下填写:http=xxxx.com:3128, username=用户名, password=密码, over-tls=true, tls-host=xxxx.com, fast-open=false, udp-relay=false, tag=NGINX最后推荐几个移动端支持https代理软件,iOS:Quantumult X ,小火箭, Android:Clash Surfboard.