咨询热线:4006-75-4006

售前:9:00-23:30    备案:9:00-18:00    技术:7*24h

Nginx正向代理

2026-03-02 19:09:25 129次

Nginx正向代理

欢迎来到8455线路检测中心技术小课堂,每天分享一个技术小知识。

1. 概述 

Nginx是一款高性能的HTTP和反向代理服务器,同时也支持正向代理功能。正向代理(Forward Proxy)是位于客户端和原始服务器之间的服务器,客户端通过正向代理访问外部资源,通常用于缓存加速、访问控制、内容过滤等场景。

2. 正向代理与反向代理的区别 

image.png

3. 正向代理核心配置 

3.1 基础HTTP正向代理配置 

# 定义缓存路径

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m max_size=1g inactive=60m;


# 代理服务器配置

server {

    listen 8080;

    server_name proxy.example.com;    

    # DNS解析设置

    resolver 8.8.8.8 114.114.114.114 valid=300s;

    resolver_timeout 5s;

    

    # 代理设置

    location / {

        # 允许所有客户端访问

        allow all;

        

        # 代理配置

        proxy_pass $scheme://$http_host$request_uri;

        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        

        # 缓存配置

        proxy_cache proxy_cache;

        proxy_cache_valid 200 302 10m;

        proxy_cache_valid 404 1m;

        proxy_cache_use_stale error timeout invalid_header updating;

        

        # 连接超时设置

        proxy_connect_timeout 30s;

        proxy_send_timeout 60s;

        proxy_read_timeout 60s;

    }

    

    # 健康检查端点

    location /health {

        access_log off;

        return 200 "healthy\\n";

    }

}

3.2 HTTPS正向代理配置 

Nginx原生不支持HTTPS正向代理,需要通过第三方模块或特殊配置实现:

# 需要ngx_http_proxy_connect_module模块支持

server {    listen 3128;

    server_name proxy.example.com;

    

    # 允许CONNECT方法(HTTPS隧道)

    allow 192.168.1.0/24;

    deny all;

    

    location / {

        # HTTP代理

        proxy_pass http://$host$request_uri;

        proxy_set_header Host $host;

    }

    

    # HTTPS隧道

    location / {

        proxy_connect;

        proxy_connect_allow 443 563;

        proxy_connect_connect_timeout 20s;

        proxy_connect_read_timeout 20s;

        proxy_connect_send_timeout 20s;

    }

}

4. 访问控制配置 

4.1 IP白名单控制 

location / {    # 允许内部网络访问

    allow 192.168.1.0/24;

    allow 10.0.0.0/8;

    deny all;

    

    proxy_pass $scheme://$host$request_uri;

}

4.2 域名过滤 

map $host $blocked_domain {

    default 0;

    ~*porn 1;

    ~*gambling 1;

    ~*malware 1;

}


server {

    listen 8080;

    

    location / {

        if ($blocked_domain) {            return 403 "Access Denied";

        }

        proxy_pass $scheme://$host$request_uri;

    }

}

5. 缓存优化配置 

5.1 智能缓存策略 

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=proxy_cache:100m                  max_size=10g inactive=60m use_temp_path=off;

server {    listen 8080;

    

    location / {

        proxy_cache proxy_cache;

        

        # 缓存键策略

        proxy_cache_key "$scheme$proxy_host$request_uri";

        

        # 缓存时间设置

        proxy_cache_valid 200 302 1h;

        proxy_cache_valid 301 1d;

        proxy_cache_valid any 1m;

        

        # 缓存控制

        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

        proxy_cache_revalidate on;

        proxy_cache_min_uses 2;

        proxy_cache_lock on;

        proxy_cache_lock_timeout 5s;

        

        # 忽略Set-Cookie头

        proxy_ignore_headers Set-Cookie;

    }

}

6. 日志配置 

http {    log_format proxy_format '$remote_addr - $remote_user [$time_local] '

                           '"$request" $status $body_bytes_sent '

                           '"$http_referer" "$http_user_agent" '

                           'proxy: "$proxy_host" upstream: "$upstream_addr"';

    

    access_log /var/log/nginx/proxy_access.log proxy_format;

    error_log /var/log/nginx/proxy_error.log warn;

}

7. 性能调优 

7.1 连接池优化 

proxy_http_version 1.1;

proxy_set_header Connection "";

keepalive_timeout 75s;

keepalive_requests 100;

# 上游连接保持

proxy_buffers 8 16k;

proxy_buffer_size 32k;

proxy_busy_buffers_size 64k;

7.2 超时设置 

proxy_connect_timeout 5s;

proxy_send_timeout 20s;

proxy_read_timeout 30s;

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

proxy_next_upstream_tries 3;

8. 安全配置 

8.1 限制连接速率 

limit_conn_zone $binary_remote_addr zone=proxy_conn:10m;

limit_req_zone $binary_remote_addr zone=proxy_req:10m rate=10r/s;


server {    listen 8080;

    

    location / {

        limit_conn proxy_conn 10;

        limit_req zone=proxy_req burst=20 nodelay;

        

        proxy_pass $scheme://$host$request_uri;

    }

}

8.2 防止代理滥用 

# 禁止代理到内部地址

location / {    set $block 0;

    

    # 检查目标是否为内网地址

    if ($host ~* "^(10\\.|172\\.(1[6-9]|2[0-9]|3[0-1])\\.|192\\.168\\.)") {

        set $block 1;

    }

    

    if ($block = 1) {

        return 403 "Internal network access not allowed";

    }

    

    proxy_pass $scheme://$host$request_uri;

}

9. 客户端配置示例 

9.1 Linux系统 

# 临时设置export http_proxy="http://www.landui.com:8080"export https_proxy="http://www.landui.com:8080"export no_proxy="localhost,127.0.0.1,192.168.1.0/24"

# 永久设置(/etc/profile或~/.bashrc)echo 'export http_proxy="http://www.landui.com:8080"' >> ~/.bashrcecho 'export https_proxy="http://www.landui.com:8080"' >> ~/.bashrc

9.2 Windows系统 

# PowerShell设置[Environment]::SetEnvironmentVariable("HTTP_PROXY", "http://www.landui.com:8080", "User")

[Environment]::SetEnvironmentVariable("HTTPS_PROXY", "http://www.landui.com:8080", "User")

10. 监控与维护 

10.1 状态监控 

location /nginx_status {

    stub_status on;    access_log off;

    allow 127.0.0.1;

    deny all;

}

location /proxy_status {

    proxy_cache_status on;

    access_log off;    allow 192.168.1.0/24;

    deny all;

}

10.2 缓存清理脚本 

#!/bin/bash# 清理过期缓存

find /var/cache/nginx -type f -delete

# 重新加载配置

nginx -s reload

11. 故障排除 

常见问题及解决方案: 

代理连接失败

检查DNS解析:nslookup target.com

验证网络连通性:telnet proxy-server 8080

缓存不生效

检查缓存路径权限

查看缓存头信息:curl -I http://www.landui.com

性能问题

监控连接数:netstat -an | grep :8080 | wc -l

检查系统资源:top, free -h


8455线路检测中心官网上拥有完善的技术支持库可供参考,大家可自行查阅,更多技术问题,可以直接咨询。同时,8455线路检测中心整理了运维必备的工具包免费分享给大家使用,需要的朋友可以直接咨询。

更多技术知识,8455线路检测中心期待与你一起探索。


首页
最新活动
个人中心
XML 地图