局域网机器配置Nginx正向代理访问外网服务

正向代理

1 简介

2 请求逻辑

+-----------------+        +-----------------+        +-----------------+
| 内网集群客户端  |        | Nginx代理服务器 |        | 外网目标服务器  |
+-----------------+        +-----------------+        +-----------------+
|                 |        |                 |        |                 |
| 1. 发送HTTP请求 |------->| 2. 接收HTTP请求 |------->| 3. 处理HTTP请求 |
|                 |<-------|                 |<-------|                 |
| 4. 接收HTTP响应 |        | 5. 发送HTTP响应 |        | 6. 返回HTTP响应 |
|                 |        |                 |        |                 |
+-----------------+        +-----------------+        +-----------------+

3. 搭建使用

正向代理配置nginx.conf

step1.首先准备好一个正向代理配置文件nginx.conf,放到某个位置,如:/data/xiaoming/nginx/nginx.conf

#user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       8888;
        resolver 8.8.8.8;
        proxy_connect;
        proxy_connect_allow           443 563;
        proxy_connect_connect_timeout  90s;
        proxy_connect_read_timeout     90s;
        proxy_connect_send_timeout     90s;
        location / {
            proxy_pass http://$host;
            proxy_set_header Host $host;
        }
    }
}

step2. 在有外网环境的机器启动Nginx,例如IP是10.21.190.11

docker run --name nginx -itd  -v /data/xiaoming/nginx/nginx.conf:/etc/nginx/nginx.conf -p 8888:8888  zzc932/nginx:https_proxy

在内网集群设置代理

export http_proxy=http://10.21.190.11:8888
export https_proxy=http://10.21.190.11:8888

验证

curl www.baidu.com
apt-get update 或 yum update
posted @ 2023-03-28 19:08  钟子期  阅读(1366)  评论(0)    收藏  举报