搭建Nginx正向代理服务


需求背景:

前段时间公司因为业务需求需要部署一个正向代理,需要内网服务通过正向代理访问到外网移动端厂商域名通道等效果,之前一直用nginx做四层或者七层的反向代理,正向代理还是第一次配置,配置的过程也遇到些小坑,今天就分享出来。

安装环境准备:

nginx本身是不支持https协议请求转发,为了让nginx能达到这一效果需要借助第三方模块ngx_http_proxy_connect_module。首先下载这一模块:https://github.com/chobits/ngx_http_proxy_connect_module到服务器,然后准备nginx环境

yum -y install pcre-devel zlib-devel gcc gcc+c++ make openssl-devel pcre-devel  zlib-devel patch   
tar xf nginx-1.6.0.tar.gz
unzip /root/ngx_http_proxy_connect_module-master.zip
cd  /root/nginx-1.6.0/
patch  -p1 < /root/ngx_http_proxy_connect_module-master/proxy_connect.patch  
./configure --add-module=/root/ngx_http_proxy_connect_module-master/ngx_http_proxy_connect_module
make &&  make install

编译安装成功后,配置nginx正向代理:

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;

server {
        listen 88;                #监听端口
        resolver 183.60.82.98;   #dns解析地址
        server_name  _;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
             proxy_pass https://$host$request_uri;     #设定http代理服务器的协议和地址 
             proxy_set_header HOST $host;
             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;
            #root   html;
            #index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }



server {
       resolver 8.8.8.8;   #dns解析地址
       listen 89;          #代理监听端口
       proxy_connect;
       proxy_connect_allow            443 563;
       location / {
             proxy_pass https://$host$request_uri;     #设定https代理服务器的协议和地址 
             proxy_set_header HOST $host;
             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;

       }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }   

}

当配置文件配置好之后保存即可,重启nginx,进行测试:

去内网服务器里curl,可以在环境变量里添加代理:

vim  /etc/profile
...
...
#export https_proxy=正向代理IP:端口
export https_proxy=192.168.3.17:89

另一种 方式不用加环境变量,临时代理

#curl -i  --proxy 代理IP:端口      要访问域名
curl -i  --proxy 192.168.3.17:89      www.baidu.com


原文链接:https://www.cnblogs.com/Dfengshuo/archive/2019/11/22/11911406.html


登录 - 评论

评论