HTTP CONNECT隧道七层正向代理
Nginx七层正向代理
前言
位于客户端和目标服务器之间的Nginx正向代理服务器,客户端向Nginx正向代理发送一个请求并指定目标服务器,然后代理向目标服务器转交请求并将获得的内容返回给客户端及本地代理服务器缓存。
正向代理Http请求
# 10.122.10.98,七层代理,在http模块下添加,正向代理转发http请求
server {
# 监听8080端口
listen 8080;
# 指定DNS服务器IP地址
resolver 114.114.114.114;
# 服务器IP或域名
server_name localhost;
# 正向代理转发http请求
location / {
# 发送http请求,实际转发到http请求,直接发送https请求则会失败
proxy_pass http://$host$request_uri;
# 发送http请求,实际转发到https请求,直接发送https请求则会失败
# proxy_pass https://$host$request_uri;
# $http_host带端口转发,某个网站域名为http://xxx.com:8080/xxx,通过设置hosts后可以正向代理请求
# proxy_pass http://$http_host$request_uri;
proxy_set_header HOST $host;
# 传递真实客户端IP
proxy_set_header X-Real-IP $remote_addr;
# 传递端口
proxy_set_header X-Real-Port $remote_port;
# 传递经过代理链的客户端IP列表
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 传递请求协议(http/https)
proxy_set_header X-Forwarded-Proto $scheme;
}
}
通过设置系统代理,或者curl http://www.baidu.com -v -x 10.122.10.98:8080访问。
正向代理Https请求
Nginx官方版本并不直接支持HTTPS请求的转发,需要ngx_http_proxy_connect_module 模块支持,安装参考。
# 10.122.10.98,七层代理,在http模块下添加,转发http、https请求
server {
listen 8080;
# dns resolver used by forward proxying
resolver 114.114.114.114;
# forward proxy for CONNECT request
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
# forward proxy for non-CONNECT request
location / {
# 同时支持http和https协议,发送http请求时,如果远程服务器不支持http协议,需要带-L重定向参数
proxy_pass $scheme://$host;
# 不管发送http、https请求,实际转发到https请求,发送http请求时,如果远程服务器不支持http协议,无需带-L重定向参数
# proxy_pass https://$host;
# 不管发送http、https请求,https请求可以直接转发,发送http请求时,如果远程服务器不支持http协议,需要带-L重定向参数
# proxy_pass http://$host;
proxy_set_header Host $host;
}
}
在另外一台机器,通过设置系统代理,或者curl http://www.baidu.com -v -x 10.122.10.98:8080,或者curl https://www.baidu.com -v -x 10.122.10.98:8080访问。
正向代理转发两次
Client Machine C –> Nginx Machine B –> Nginx Machine A(installed connect_module) –> target website
在机器B配置好正向代理转发配置如下,机器A正向代理转发配置了connect_module如上节所示 。
# 机器B,10.122.10.218,内网二次转发,只支持转发http请求,proxy_connect模块无法转发https请求
server {
listen 8081;
#server_name localhost;
#resolver 10.122.10.98 ipv6=off;
#proxy_connect;
#proxy_connect_allow 443 80;
#proxy_connect_connect_timeout 10s;
#proxy_connect_read_timeout 10s;
#proxy_connect_send_timeout 10s;
location / {
proxy_pass $scheme://10.122.10.98:8081;
# proxy_pass http://10.122.10.98:8080;
proxy_set_header Host $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;
}
}
在另外一台机器,通过设置系统代理,或者curl http://www.baidu.com -v -x 10.122.10.218:8080访问正常,curl https://www.baidu.com -v -x 10.122.10.218:8080访问不正常。如果需要访问https协议,可以通过发送http请求,机器A强制正向代理到https协议。
Client Machine C –> Nginx Machine B –> Nginx Machine A –> target website
另一种方式是前面机器B配置好正向代理请求http,机器A再配置正向代理将http请求强制转为https请求,都无需安装ngx_http_proxy_connect_module模块,发送请求的时候通过http协议。
Linux设置代理
# 编辑
vim /etc/profile
export http_proxy=http://proxy_address:port
export https_proxy=https://proxy_address:port
# 设置
export http_proxy=http://lideyu:lideyu@10.122.10.98:808
export https_proxy=http://lideyu:lideyu@10.122.10.98:808
# 重新加载系统环境变量文件
source /etc/profile
# 查看环境变量
env
# 取消代理,profile注释代理后执行生效
unset http_proxy
unset https_proxy