linux服务器配置http和https正向代理

一、

安装编译环境和工具

yum install gcc gcc-c++ autoconf automake -y

yum install pcre pcre-devel -y

yum install openssl openssl-devel -y

yum install patch -y

yum install git -y

yum install net-tools -y

二、

安装Nginx和ngx_http_proxy_connect_module模块

mkdir -p /downloads

cd /downloads

git clone https://github.com/chobits/ngx_http_proxy_connect_module.git

wget http://nginx.org/download/nginx-1.18.0.tar.gz

tar -xzvf nginx-1.18.0.tar.gz

cd nginx-1.18.0/

patch -p1 < /downloads/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch

./configure --add-module=/downloads/ngx_http_proxy_connect_module

make && make install

nginx版本和新增模块版本的对应关系如下,不要弄错了:

三、

修改Nginx配置文件

Nginx目录:/usr/local/nginx/conf/nginx.conf

在空白处插入以下的代码,8081是用来代理http请求,8082用来代理https请求

server {  
    resolver 114.114.114.114; 
    listen 8081;  
    location / {  
        proxy_pass http://$http_host$request_uri;
        proxy_set_header HOST $http_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;
    }  
}

server {
     listen                         8082;
     # dns resolver used by forward proxying
     resolver                       114.114.114.114;
     # forward proxy for CONNECT request

     proxy_connect;
     proxy_connect_allow            443 563;
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request

     location / {
         proxy_pass http://$host;
         proxy_set_header Host $host;
     }
 }

四、开启nginx

开启      ./sbin/nginx

重启      ./sbin/nginx -s reload

关闭      ./sbin/nginx -s stop

五、查看端口是否在使用

netstat -tnlp | grep 8081

netstat -tnlp | grep 8082

六、如果端口关闭,需要打开,如果开启无效,看下服务器平台端口是否打开

firewall-cmd --zone=public --add-port=8081/tcp

firewall-cmd --zone=public --add-port=8081/tcp --permanent

firewall-cmd --zone=public --add-port=8082/tcp

firewall-cmd --zone=public --add-port=8082/tcp --permanent

重启刷新:

firewall-cmd --reload

七、测试

curl --proxy 你的服务器ip:8081 http://www.baidu.com

curl --proxy 你的服务器ip:8082 http://www.baidu.com

以上两种测试方式,返回数据如下,成功!!!

原文地址:https://www.cnblogs.com/qczy/p/14141505.html