NGINX编译ngx_http_proxy_connect_module及做正向代理

NGINX编译ngx_http_proxy_connect_module

1、下载NGINX(网址:http://nginx.org/download/ 当前文档使用版本为1.22.1)及ngx_http_proxy_connect_module模块
[root@localhost work]# tar xzf nginx-1.22.1.tar.gz
[root@localhost work]# cd nginx-1.22.1
[root@localhost nginx-1.22.1]# git clone https://gitee.com/web_design_of_web_frontend/ngx_http_proxy_connect_module.git

2、下载该模块的补丁包(需要通过打补丁的方式把上述下载的正向代理模块导入到nginx模块的存储目录中)
[root@localhost work]# wget https://gitcode.net/mirrors/chobits/ngx_http_proxy_connect_module/-/archive/master/ngx_http_proxy_connect_module-master.tar.gz
[root@localhost work]# tar xzf ngx_http_proxy_connect_module-master.tar.gz
[root@localhost work]# cd ngx_http_proxy_connect_module-master/patch
在这里插入图片描述

注:以下为各补丁包兼容版本
在这里插入图片描述

[root@localhost work]# patch -p1 < /root/work/ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_102101.patch
在这里插入图片描述

注:File to patch处应该填写上方+++后面提示的ngx_http_core_module.c所在的目录(本机所在目录为/root/work/nginx-1.22.1/src/http/ ngx_http_core_module.c)

3、进入到nginx-1.22.1目录下编译nginx
[root@localhost nginx-1.22.1]# ./configure
–prefix=/usr/local/nginx \
–sbin-path=/usr/local/nginx/sbin/nginx
–conf-path=/usr/local/nginx/conf/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–pid-path=/var/run/nginx.pid
–lock-path=/var/run/nginx.lock
–user=nginx --group=nginx
–http-client-body-temp-path=/var/cache/nginx/client_temp
–http-proxy-temp-path=/var/cache/nginx/proxy_temp
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
–http-scgi-temp-path=/var/cache/nginx/scgi_temp
–with-http_ssl_module
–with-http_realip_module
–with-http_addition_module
–with-http_sub_module
–with-http_dav_module
–with-http_flv_module
–with-http_mp4_module
–with-http_gunzip_module
–with-http_gzip_static_module
–with-http_random_index_module
–with-http_secure_link_module
–with-http_stub_status_module
–with-http_auth_request_module
–with-http_image_filter_module
–with-http_geoip_module
–with-http_slice_module
–with-http_v2_module
–with-threads
–with-stream
–with-stream_ssl_module
–with-mail
–with-mail_ssl_module
–with-file-aio
–with-compat
–add-dynamic-module=./ModSecurity-nginx
–add-dynamic-module=./nginx-rtmp-module
–add-dynamic-module=./nginx-module-vts
–add-dynamic-module=./ngx_http_proxy_connect_module #添加该模块进行编译
[root@localhost nginx-1.22.1]# make
注:做升级或第二次编译处理时,不要做make install 操作,可能会覆盖掉之前的配置,make后直接替换二进制文件

4、替换nginx的二进制文件
[root@localhost objs]# cp /root/work/nginx-1.22.1/objs/nginx /user/local/nginx/sbin/
[root@localhost objs]# cp /root/work/nginx-1.22.1/objs/ngx_http_proxy_connect_module.so /usr/local/nginx/modules/
注:替换二进制文件前,停掉nginx服务,将之前的二进制文件备份到其他目录

5、配置nginx
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
主配置文件内添加:load_module modules/ngx_http_proxy_connect_module.so;
在这里插入图片描述

域名配置文件如下:

server {
    listen 8848;    #对外服务端口
    charset utf-8;
    resolver 114.114.114.114;    #配置DNS解析
    client_max_body_size  50m;
    access_log /data/log/outer_yunpian.com.log main_json;

    proxy_connect;    #开启该模块功能
    proxy_connect_allow            443 80;    #允许通过nginx访问该域名80/443端口
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;

   location / {
       proxy_pass $scheme://$http_host$request_uri;    #设置代理地址
       proxy_set_header Host $host:$server_port;
       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 http;
       proxy_buffer_size 512k;
       proxy_buffers 6 512k;
       proxy_busy_buffers_size 512k;
       proxy_temp_file_write_size 512k;
     }

   location ~ /\.ht {
     deny all;
    } 

}

6、启动nginx服务

7、在需要做正向代理的客户端服务器上配置环境变量进行测试
[root@node2 ~]# vi /etc/profile
在这里插入图片描述

[root@node2 ~]# source /etc/profile
添加:
export http_proxy=http://192.168.1.11:8848
export https_proxy=http://192.168.1.11:8848
以上为nginx服务器IP及其代理端口
或者使用以下命令测试:curl --proxy 192.168.1.11:8848 http://www.yunpian.com
若添加了环境变量,则直接使用curl http://www.yunpian.com进行测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值