nginx正向代理服务器搭建

nginx正向代理服务器搭建

场景:由于网络环境限制,只能通过代理服务器访问外网,代理服务器自己部署、由于业务需求容器或者k8s的pod也需要通过代理访问外网

代理服务器搭建:系统CentOS 7,通过编译安装nginx实现正向代理功能
代理服务器(CentOS 7):180.76.189.xxx 192.168.0.15
内网服务器(Ubuntu16.04):192.168.0.150

步骤如下:
1、编译安装nginx

yum -y install gcc gcc-c++ autoconf automake pcre pcre-devel openssl openssl-devel patch git net-tools

mkdir -p /downloads && cd /downloads

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

git clone https://github.com/chobits/ngx_http_proxy_connect_module.git

cd nginx-1.17.2

patch -p1 

2、把nginx配置systemctl管理

#cat vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

3、配置nginx代理规则

cp /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak

vi  /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes auto;
worker_rlimit_nofile 65535;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  20480;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
server {
    resolver 8.8.8.8;
    listen 8080;
    location / {
        proxy_pass http://$http_host$request_uri;
        proxy_set_header HOST $http_host;
        proxy_buffers 256 4k;
        proxy_max_temp_file_size 0k;
        proxy_connect_timeout 30;
        proxy_send_timeout 60;
        proxy_read_timeout 60;
        proxy_next_upstream error timeout invalid_header http_502;
    }
}


server {
     listen                         8443;

     # dns resolver used by forward proxying
     resolver                       114.114.114.114;

     # forward proxy for CONNECT request
     proxy_connect;
     proxy_connect_allow            443 563;
     proxy_connect_connect_timeout  10s;
     proxy_connect_read_timeout     10s;
     proxy_connect_send_timeout     10s;

     # forward proxy for non-CONNECT request
     location / {
         proxy_pass http://$host;
         proxy_set_header Host $host;
     }
 }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


#/usr/local/nginx/sbin/nginx -t
 #/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 #/usr/local/nginx/sbin/nginx -s reload
systemctl enable nginx
systemctl start nginx

内网服务器配置代理
1、系统代理

cat /etc/profile  && source /etc/profile

http_proxy=http://192.168.0.15:8080/
https_proxy=https://192.168.0.15:8443/
ftp_proxy=http://192.168.0.15:8080/
export http_proxy
export https_proxy
export ftp_proxy

2、wget代理

cat /etc/wgetrc

http_proxy=http://192.168.0.15:8080/
https_proxy=https://192.168.0.15:8443/
ftp_proxy=http://192.168.0.15:8080/

3、配置apt代理

cat /etc/apt/apt.conf  && apt-get update

Acquire::http::proxy "http://192.168.0.15:8080/";
Acquire::https::proxy "https://192.168.0.15:8443/";
Acquire::ftp::proxy "ftp://192.168.0.15:8080/";

4、安装docker

 apt-get -y install apt-transport-https ca-certificates curl software-properties-common	
  curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
  add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"	
  apt-get -y update
  apt-cache madison docker-ce
  apt-get -y install docker-ce=18.06.3~ce~3-0~ubuntu

5、配置docker代理

mkdir -p  /etc/systemd/system/docker.service.d/

cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.0.15:8080"
Environment="HTTPS_PROXY=http://192.168.0.15:8443"
Environment="NO_PROXY=*.test.example.com,.example2.com"

mkdir .docker
cat .docker/config.json
{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://192.168.0.15:8080",
     "httpsProxy": "http://192.168.0.15:8443",
     "noProxy": "*.test.example.com,.example2.com"
   }
 }
}


systemctl daemon-reload
systemctl restart docker

你可能感兴趣的:(Linux,nginx)