美文网首页
正向代理

正向代理

作者: 薄荷盐 | 来源:发表于2023-02-26 20:44 被阅读0次

    nginx

    nginx: http://nginx.org/packages/centos/7/x86_64/RPMS/

    在线安装

    yum install nginx -y
    

    常用命令

    # 查看版本
    nginx -V
    # 启动命令
    nginx
    # 检查配置
    nginx -t
    # 重新加载配置
    nginx -s reload
    # 停止
    nginx -s stop
    

    离线安装

    下载 rpm 包到 /opt 目录下,然后安装
    nginx 依赖 libpcre2 ,还需要下载 libpcre2

    rpm -ivh pcre2-10.23-2.el7.x86_64.rpm
    rpm -ivh nginx-1.22.1-1.el7.ngx.x86_64.rpm
    

    检查服务

    # 检查版本模块
    [root@VM-0-17-centos ~]# nginx -V
    nginx version: nginx/1.22.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/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 --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 --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    

    tengine

    tengine: http://tengine.taobao.org/download.html

    tengine 已经内置了 ngx_http_proxy_connect_module 模块,不需要从外部加载

    # 如果安装了nginx,先卸载
    rpm -qa|grep nginx
    rpm -e nginx-1.20.2-1.el7.ngx.x86_64
    # 编译安装
    # 下载源码包到/opt 目录下
    tar zxf tengine-2.3.3.tar.gz
    cd tengine-2.3.3/
    ./configure --prefix=/opt/nginx --add-module=./modules/ngx_http_proxy_connect_module
    make && make install
    

    检查服务

    [root@VM-0-17-centos conf]# nginx -V
    Tengine version: Tengine/2.3.3
    nginx version: nginx/1.18.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/opt/nginx --add-module=./modules/ngx_http_proxy_connect_module
    

    正向代理配置

    nginx 官方没有支持正向代理的模块,只能通过加载第三方模块来实现

    • 安装依赖
    yum -y install pcre-devel openssl openssl-devel
    
    • 下载二进制包
    https://nginx.org/download/nginx-1.22.1.tar.gz
    https://github.com/chobits/ngx_http_proxy_connect_module/archive/refs/tags/v0.0.3.tar.gz
    
    • 安装 patch
    yum install -y patch
    
    • 编译安装
    # 将nginx和ngx_http_proxy_connect_module解压到 /opt 目录下
    [root@VM-0-17-centos opt]# ls
    ngx_http_proxy_connect_module nginx-1.22.1
    # 加载 ngx_http_proxy_connect_module
    cd nginx-1.22.1/
    patch -p1 < /opt/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch
    ./configure --prefix=/opt/nginx --add-module=/opt/ngx_http_proxy_connect_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module
    make && make install
    
    • 增加正向代理配置
    server {
        listen                           8080;
        server_name                      localhost;
        resolver                         114.114.114.114;
        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://$http_host$request_uri;
        }
    }
    
    • 代理测试
    [root@hdp101 ~]# curl -I --proxy 172.18.0.17:8080  http://cip.cc
    HTTP/1.1 200 OK
    Server: Tengine/2.3.3
    Date: Wed, 07 Dec 2022 06:40:48 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Vary: Accept-Encoding
    X-cip-c: M
    [root@hdp101 ~]# curl -I --proxy 172.18.0.17:8080  https://cip.cc
    HTTP/1.1 200 OK
    Server: Tengine/2.3.3
    Date: Wed, 07 Dec 2022 06:40:48 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    Vary: Accept-Encoding
    X-cip-c: M
    

    配置环境变量

    echo 'export PATH=$PATH:/opt/nginx/sbin' >> /etc/profile
    source /etc/profile
    

    开机启动

    chmod +x /etc/rc.d/rc.local
    echo '/opt/nginx/sbin/nginx' >> /etc/rc.d/rc.local
    

    相关文章

      网友评论

          本文标题:正向代理

          本文链接:https://www.haomeiwen.com/subject/uvmcldtx.html