nginx正向代理配置

634 阅读1分钟

说明

  • 阿里云服务器一台: Ubuntu22.04、 1核1G、位于日本东京
  • nginx版本: v1.23.0
  • openssl版本: v3.1.0

download

解压

tar zxvf nginx-1.23.0.tar.gz
tar zxvf openssl-3.1.0-beta1.tar.gz

安装nginx需要的依赖

apt-get update
apt-get install libpcre3 libpcre3-dev

configure nginx

# cd 到nginx解压后的目录
cd /root/nginx-1.23.0
# 更新 nginx 源码,
patch -p1 < /root/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_102101.patch
# configure nginx
./configure --prefix=/usr/local/nginx --add-module=/root/ngx_http_proxy_connect_module --with-http_ssl_module --with-openssl=/root/openssl-3.1.0-beta1

install nginx

cd /root/nginx-1.23.0
make && make install

启动nginx

# nginx版本
/usr/local/nginx/sbin/nginx -v
# 启动nginx
/usr/local/nginx/sbin/nginx

更新 nginx 配置

vim /usr/local/nginx/conf/nginx.conf

server {
    listen                         80;

    resolver                       8.8.8.8;
    proxy_connect;
    proxy_connect_allow            443 563;
    proxy_connect_connect_timeout  10s;
    proxy_connect_data_timeout     10s;
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;
    }
}

image.png

重启 nginx

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

测试正向代理是否生效

# 服务器上测试
curl -x http://127.0.0.1:80 -L https://www.baidu.com
curl -x http://127.0.0.1:80 -L http://www.baidu.com

# 本机测试
curl -x http://47.245.30.201:80 -L http://www.baidu.com
curl -x http://47.245.30.201:80 -L https://www.baidu.com

3e6a1c92cadf66ccfdb81e0cb1e88a4.png

6e30ee36410b2d94ce23d426373192c.png

firefox配置代理测试

  • 点击 firefox 设置 > 常规 > 网络设置

a49d6da3a7fb4faa1023deeb86466f0.png

  • 勾选手动配置代理, http和https代理配置为正向代理的ip和端口 9fcee8573d11bee7660838ffc53c6f7.png

  • 下图是我的服务器的地理位置,位于日本东京 593ad8a29a3db3714f5ba70028a9415.png

  • firefox 在 baidu 搜索ip,可以看到当前ip地址是我的服务器ip,位置是日本东京 阿里云,说明firefox的网络请求经过了服务器的nginx代理服务器 8485247ddbdfefe78ae828fce9b86ba.png