nginx代理上网

本文详细介绍了如何在外网IP为10.0.0.41,内网IP为172.16.1.41的环境下,通过安装Nginx并进行配置实现正向代理。步骤包括关闭防火墙、安装Nginx、修改配置文件以添加正向代理规则,以及在无网客户端上设置全局代理。最后通过curl命令测试代理是否成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备
关闭防火墙

systemctl stop firewalld
setenforce 0

一、配置nginx正向代理

1、下载ngx_http_proxy_connect_module 模块,Nginx 官方版本不支持 CONNECT,需安装 ngx_http_proxy_connect_module 模块

cd /usr/local
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git

2、下载nginx源码包

通过网盘分享的文件:nginx-1.18.0.tar.gz
链接: https://pan.baidu.com/s/1pXSMVjriwaL4Ch0H9aOrRA?pwd=phx3 提取码: phx3

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

3、打补丁

通过网盘分享的文件:ngx_http_proxy_connect_module.tar.gz
链接: https://pan.baidu.com/s/1YOAqNTBPncU4lHBFz8bJsQ?pwd=bed1 提取码: bed1

cd nginx-1.18.0
patch -p1 < /usr/local/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
#对应的版本补丁 1018对应nginx 1.18.0

在这里插入图片描述

4、编译安装模块

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads --with-stream --add-module=/usr/local/ngx_http_proxy_connect_module
make
make install

5、配置nginx http,https代理上网

cat > /usr/local/nginx/conf/nginx.conf <<'EOF'
worker_processes  auto;

events {
    worker_connections  1024;
}

http {
    server {
        listen 18088;
        resolver 8.8.8.8 114.114.114.114 valid=30s;  # 增加备用DNS和缓存时间

        # 启用CONNECT方法(HTTPS代理)
        proxy_connect;
        proxy_connect_allow all;  # 允许所有端口
        proxy_connect_connect_timeout 10s;
        proxy_connect_read_timeout 10s;
        proxy_connect_send_timeout 10s;

        location / {
            proxy_pass http://$http_host$request_uri;  # 使用$http_host更准确
            proxy_set_header Host $host;
            proxy_buffering off;  # 禁用缓冲提高实时性
        }
    }
}
EOF

6、检查配置并启动nginx

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

在这里插入图片描述

7、客户端使用代理​

配置完成后,客户端可通过以下方式使用代理:
​Linux/Unix​

export http_proxy=http://proxy_ip:18088
export https_proxy=http://proxy_ip:18088

没做代理之前
在这里插入图片描述
做了代理之后
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值