自建代理IP
背景
一般代理分为正向的和反向的代理,这里不展开讲,因为我最近刚刚好在弄爬虫需要代理IP,所以下文就讲如何使用Nginx搭建一个正向的代理IP。
前置条件
- 一台有公网IP的云服务器
- 防火墙的端口已经开放
- ubuntu22.04(非必要,只是我的机器是这个)
安装Ningx
提示
- nginx 正向代理默认只支持 http,
- https 的支持需借助第三方模块 ngx_http_proxy_connect_module实现;
代理http
安装
如果只代理http可以直接通过yum或者apt安装nginx.
配置
server { |
代理https
代理https就需要添加上面说的ngx_http_proxy_connect_module三方模块
安装
- 安装依赖
apt -y install make gcc openssl libssl-dev libxml2 libxml2-dev libxslt-dev build-essential libpcre3 libpcre3-dev zlib1g-dev libbrotli1 libbrotli-dev libgd-dev libgeoip-dev
- 下载nginx和三方模块ngx_http_proxy_connect_module并解压
网速慢可以官网下载后上传
wget https://nginx.org/download/nginx-1.24.0.tar.gz |
- 添加改模块
注意这里对应的依赖
cd nginx-1.24.0.tar.gz/
patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --with-stream_realip_module --with-http_geoip_module --add-module=/opt/ngx_http_proxy_connect_module - 编译安装
make -j4 && make install
- 添加环境变量和systemctl
cp /usr/local/nginx/sbin/nginx /bin/
cp /usr/local/nginx/sbin/nginx /usr/sbin/nginx
cat >> /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
systemctl status nginx.service
配置
server { |
测试
curl --proxy xxx.xxx.xxx.xxx:xx http://www.baidu.com |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 和而不同!
评论
ValineDisqus