Nginx 作为代理服务器:正向代理HTTP/HTTPS的配置与实现
Nginx 作为代理服务器:正向代理HTTP/HTTPS的配置与实现
配置 http 代理
server {
listen 7003; # 监听端口可以根据需要更改
server_name localhost; # 你的域名或IP地址
resolver 8.8.8.8; # DNS 解析器
resolver_timeout 5s;
location / {
proxy_pass $scheme://$http_host$request_uri; # 转发请求
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
注意:用 $http_host 而不是 $host ,$host 获取不到代理地址的端口号导致到跳转到带端口号的网址时 proxy_pass 的地址失败
重启 nginx 服务
.\nginx.exe -s reload
测试 http 代理,cmd 下运行
curl -x http://192.168.5.116:7003 http://big5.www.gov.cn/gate/big5/www.gov.cn/ -i -L -v
curl -x http://192.168.1.199:3000/api/proxy http://192.168.1.199:3000/api/proxy -i -L -v
配置 https 代理
server {
listen 443 ssl;
server_name proxy.com;
# SSL证书配置
ssl_certificate C:\Users\PC\.ssh\server.crt; # 替换为你的证书路径
ssl_certificate_key C:\Users\PC\.ssh\server.key; # 替换为你的私钥路径
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议。
ssl_prefer_server_ciphers on;
location / {
proxy_pass $scheme://$http_host$request_uri; # 转发请求
proxy_set_header Host $http_host;
proxy_set_header Referer $http_referer;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
重启 nginx 服务
.\nginx.exe -s reload
ssl 证书制作可参照我的另两篇文章
安装证书:
ubuntu:
sudo cp ca.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
windows:
点击安装,证书存储选择 受信任的根证书机构
# window设置hosts并生效
hosts
127.0.0.1 proxy.com www.proxy.com
ipconfig /flushdns
# ubuntu 设置Hosts并生效
etc/hosts
192.168.5.116 proxy.com www.proxy.com
sudo systemd-resolve --flush-caches
如果是 wsl2 就用 wsl --shutdown 关闭并重启 wsl2
sudo systemd-resolve --flush-cache
代理换个地址或端口后就报下面的错说明代理是起作用了。
如果要想不用 443 端口可以这么写:
listen 3128 ssl http2;
代理服务器地址还是:https://proxy.com
如果提示:nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead
说明语法过时了,换成下面这种
listen 3128 ssl;
http2 on;
打开 https://proxy.com:3128/ 网站可以看到能打开,但是 https://proxy.com/ 是打不开的,避免占用端口。
可将下面配置将 http 转为 https
server {
listen 80;
listen [::]:80;
server_name proxy.com;
# Redirect all HTTP requests to HTTPS
return 301 https://$host$request_uri;
}
但是 wsl2 用 curl 验证的时候发现不好使,网上说是 nginx 不支持 connect 连接,需要安装第三方模块 ngx_http_proxy_connect_module,window 下的 nginx 模块需要安一堆软件,就拿 ubuntu 试试吧。windows 版看最后。
首先添加 ngx_http_proxy_connect_module 模块
server {
listen 3128 ssl;
ssl_certificate /home/hyn/server.crt;
ssl_certificate_key /home/hyn/server.key;
ssl_session_cache shared:SSL:1m;
resolver 8.8.8.8;
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_data_timeout 10s;
location / {
proxy_pass $scheme://$http_host$request_uri; # 转发请求
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo ./nginx -s reload
用 curl 测试代理
curl -x https://localhost:3128 https://mp.csdn.net/ -i -L -v
好使了。
windows版的 nginx.exe 直接下载网友编译好的吧。
https://github.com/dyq94310/nginx-build-msys2/releases
下载地址:nginx-1.25.4-x86_64-ngx_http_proxy_connect_module.exe
使用方法,下载代替原来的 exe 文件
server {
listen 3128 ssl;
http2 on;
server_name localhost;
ssl_certificate C:\Users\huyun\bb\fullchain.pem;
ssl_certificate_key C:\Users\huyun\bb\server.key;
ssl_session_cache shared:SSL:1m;
resolver 8.8.8.8;
proxy_connect;
proxy_connect_allow 443 563;
proxy_connect_connect_timeout 10s;
proxy_connect_data_timeout 10s;
location / {
proxy_pass $scheme://$http_host$request_uri; # 转发请求
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
.\nginx.exe -s reload
windows 下的 curl 总是报 吊销功能无法检查证书是否吊销,因为 windows 下的 openssl 和 ubuntu 不一样,不管了
ubuntu 下好使
测试代理
curl -x https://192.168.1.199:3128 https://www.baidu.com -i -L -v
设置 wsl2 代理
/home/hyn/.bashrc 末尾追加代理设置
export https_proxy=https://192.168.1.199:3128
source ~/.bashrc
curl https://www.baidu.com 代理正常
更多推荐
所有评论(0)