nginx实现正向代理
创始人
2025-05-28 04:01:36
0

nginx代理机器使用代理机器
192.168.0.1192.168.0.2

nginx下载地址:http://nginx.org/en/download.html
nginx https代理插件:https://github.com/chobits/ngx_http_proxy_connect_module

nginx和ngx_http_proxy_connect_module需要版本对应

nginx版本ngx_http_proxy_connect_module版本
.4.x ~ 1.12.xNOproxy_connect.patch
1.4.x ~ 1.12.xYESproxy_connect_rewrite.patch
1.13.x ~ 1.14.xNOproxy_connect_1014.patch
1.13.x ~ 1.14.xYESproxy_connect_rewrite_1014.patch
1.15.2YESproxy_connect_rewrite_1015.patch
1.15.4 ~ 1.16.xYESproxy_connect_rewrite_101504.patch
1.17.x ~ 1.18.0YESproxy_connect_rewrite_1018.patch
1.19.x ~ 1.21.0YESproxy_connect_rewrite_1018.patch
1.21.1 ~ 1.22.1YESproxy_connect_rewrite_102101.patch
1.23.xYESproxy_connect_rewrite_102101.patch

实现方式

现在大部分网站都是基于https实现的,所以我们的代理还需要实现htpps的转发,nginx基本的功能是没有https的

机器:192.168.0.1

安装依赖
yum -y install gcc gcc-c++ pcre-devel pcre zlib-devel zlib openssl-devel openssl patch
nginx安装

nginx-1.22.1.tar.gz 和 ngx_http_proxy_connect_module下载位置:/home/nginx

ngx_http_proxy_connect_module下载后名称是:master.zip

cd /home/nginx
unzip master.zip
tar -zxvf nginx-1.22.1.tar.gz 

进入到nginx解压目录。执行以下命令,如下所示便是成功了

[root@localhost nginx]# cd nginx-1.22.1/. 
[root@localhost nginx-1.22.1]# patch -p1 < /home/nginx/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch 
patching file src/http/ngx_http_core_module.c
patching file src/http/ngx_http_parse.c
patching file src/http/ngx_http_request.c
patching file src/http/ngx_http_request.h
patching file src/http/ngx_http_variables.c

开始编译安装nginx

./configure --prefix=/usr/local/nginx --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-file-aio --with-http_degradation_module --with-http_slice_module --with-stream --with-stream_ssl_module --with-http_sub_module --with-pcre --add-dynamic-module=/home/nginx/ngx_http_proxy_connect_module-master --add-module=/home/nginx/ngx_http_proxy_connect_module-master
make
make install

安装完成

配置代理server

[root@localhost nginx]# cd /usr/local/nginx/conf
[root@localhost conf.d]# vim nginx.conf

添加以下代码

server {listen       8080;resolver  114.114.114.114;proxy_connect;proxy_connect_allow            443 563 1000-50000;proxy_connect_connect_timeout  10s;proxy_connect_data_timeout     10s;location / {proxy_ssl_server_name on;proxy_set_header Host $host;proxy_set_header X-Real-Ip $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Remote_Addr $remote_addr;proxy_http_version 1.1;proxy_buffers 256 4k;proxy_max_temp_file_size 0k;proxy_connect_timeout 30;proxy_send_timeout 60;proxy_read_timeout 60;proxy_next_upstream error timeout invalid_header http_502;proxy_pass $scheme://$host$request_uri;}
}

启动

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

配置代理

机器:192.168.0.2

vim /etc/profile
source /etc/profile
PROXY_URL="http://192.168.0.1:8080/"
NO_PROXY_ADDR="127.0.0.1,localhost,.local,.cluster.local"
export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
export no_proxy="$NO_PROXY_ADDR"
export HTTP_PROXY="$PROXY_URL"
export HTTPS_PROXY="$PROXY_URL"
export NO_PROXY="$NO_PROXY_ADDR"

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...