linux服务器正向代理安装配置(HTTP/HTTPS代理)

小亮 2022-09-19 14:35 61414

nginx squid tinyproxy都可以做正向代理
nginx默认是不支持https的正向代理的, 需要额外安装一个补丁ngx_http_proxy_connect_module

安装所需要的环境

yum install -y openssl-devel zlib-devel pcre* autoconf libxml2-devel libpng-devel libjpeg-devel libXpm-devel libwebp-devel libicu-devel libicu gcc-c++ libxslt-devel freetype-devel wget curl libcurl-devel unzip cronie make patch

下载解压nginx与ngx_http_proxy_connect_module

wget https://nginx.org/download/nginx-1.20.2.tar.gz

unzip ngx_http_proxy_connect_module-0.0.2.zip

mv ngx_http_proxy_connect_module-0.0.2 ngx_http_proxy_connect_module

tar zxvf nginx-1.20.2.tar.gz

创建用户+用户组

groupadd www

useradd -s /sbin/nologin -g www www

打补丁, 编译安装nginx

cd nginx-1.20.2

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

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/ngx_http_proxy_connect_module

make && make install

配置system服务

cat > /etc/systemd/system/nginx.service << EOF

[Unit]

Description=The NGINX HTTP and reverse proxy server

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

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

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=false

[Install]

WantedBy=multi-user.target

EOF

配置文件

server {
    listen  7890;
    resolver  8.8.8.8;
    proxy_connect;
    proxy_connect_allow            443;
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;

    location / {
        proxy_pass $host;
       # proxy_pass $scheme://$http_host$request_uri;
        proxy_set_header Host $host;
    }

安装配置tinyproxy

yum install -y tinyproxy
vim /etc/tinyproxy/tinyproxy.conf

搜索Allow字段

修改成后端机器的IP段

systemctl restart tinyproxy

安装squid

squid的安装以后默认配置就能用

然后在需要使用的服务器上写入配置
vim .bashrc

export ALL_PROXY=http://$IP:$端口

PS: nginx正向代理在访问微信支付时偶尔会出现502的报错,squid正向代理访问微信支付速度比较慢