使用Nginx搭建公網代理伺服器

Yark發表於2021-09-07

使用Nginx搭建公網代理伺服器

1、代理nginx伺服器安裝

此處使用nginx-1.16.0版本

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel patch  pcre pcre-devel
wget http://nginx.org/download/nginx-1.16.0.tar.gz
git clone https://github.com/chobits/ngx_http_proxy_connect_module.git
tar xf nginx-1.16.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.16.0/
patch -p1 < /root/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_101504.patch 
#此處proxy_connect_module選擇與nginx版本有關
./configure --add-module=/root/ngx_http_proxy_connect_module/
make -j 4
#我這裡是4核的cpu
make install

安裝說明:

1、nginx正向代理預設只支援http,

2、https的支援需藉助第三方模組“ngx_http_proxy_connect_module實現;

模組GitHub地址: github.com/chobits/ngx_http_proxy_...

2、代理nginx伺服器配置

#cat /usr/local/nginx/conf/nginx.conf
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
resolver 114.114.114.114;
#DNS伺服器IP或者外網閘道器IP
    server {
        listen 6666;
        #proxy埠        
        proxy_connect;
        #呼叫ngx_http_proxy_connect_module模組
        location / {
            proxy_pass $scheme://$host$request_uri;
        }
    }    
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass $scheme://$host$request_uri;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3、客戶端配置代理伺服器上外網

3.1、Linux

配置:

vim /root/.bashrc    #http&https代理配置到使用者環境變數中去
export http_proxy=10.100.49.12:6666
export https_proxy=10.100.49.12:6666

測試:

curl  www.baidu.com
3.2、Docker

配置:

 mkdir -p /etc/systemd/system/docker.service.d
 vim /etc/systemd/system/docker.service.d/http-proxy.conf  #新增編輯docker的http代理

[Service]
 Environment="HTTP_PROXY=http://10.100.49.12:6666"

 vim /etc/systemd/system/docker.service.d/https-proxy.conf  #新增編輯docker的https代理

 [Service]
 Environment="HTTPS_PROXY=http://10.100.49.12:6666" 

docker服務過載:

 systemctl daemon-reload
 systemctl restart docker

測試:

 systemctl show --property=Environment docker  
本作品採用《CC 協議》,轉載必須註明作者和本文連結
:kissing_closed_eyes: 我愛小硯 乀(ˉεˉ乀)

相關文章