正向代理通常用于客户端通过代理服务器访问外部网络。虽然 Nginx 主要用作反向代理,但它也支持正向代理功能。下面我给你几个不同场景的正向代理配置实例。
实例一:最简单的 HTTP 正向代理
server {
listen 8888; # 代理服务器端口
location / {
# 正向代理的核心配置
resolver 8.8.8.8; # DNS 服务器,用于解析域名
# 根据请求的 URL 动态转发
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;
}
}
使用方法:
- 浏览器或应用配置代理为:
http://代理服务器IP:8888 - 所有 HTTP 请求都会通过这个 Nginx 转发
实例二:支持 HTTPS 的正向代理
HTTPS 正向代理需要使用 CONNECT 方法建立隧道:
server {
listen 8888;
# 处理 HTTP 请求
location / {
resolver 8.8.8.8;
proxy_pass $scheme://$http_host$request_uri;
proxy_set_header Host $http_host;
}
# 处理 HTTPS CONNECT 请求
location / {
# 使用 prxoy_connect 模块处理 CONNECT 方法
proxy_connect;
proxy_connect_allow 443 563; # 允许连接的端口
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
# 转发 CONNECT 请求
resolver 8.8.8.8;
proxy_pass $scheme://$http_host$request_uri;
}
}
注意:需要编译 Nginx 时添加 --with-http_proxy_connect_module 模块。
实例三:带访问控制的正向代理
server {
listen 8888;
# 允许的客户端 IP
allow 192.168.1.0/24;
allow 10.0.0.0/8;
deny all; # 其他 IP 禁止访问
# 限制访问的域名
location / {
# 禁止访问的域名
if ($http_host ~* (facebook|youtube|twitter)\.com) {
return 403 "Access Denied";
}
# 只允许特定域名
if ($http_host !~* (work\.com|company\.org)) {
return 403 "Only work-related sites allowed";
}
resolver 8.8.8.8;
proxy_pass $scheme://$http_host$request_uri;
# 添加代理标识
add_header X-Proxy-Server "Company Proxy";
}
}
实例四:带缓存的正向代理
# 定义缓存路径
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=proxy_cache:10m max_size=10g;
server {
listen 8888;
resolver 8.8.8.8;
location / {
# 只缓存 GET 请求
if ($request_method != GET) {
set $nocache 1;
}
# 缓存配置
proxy_cache proxy_cache;
proxy_cache_key "$scheme://$http_host$request_uri";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_bypass $nocache;
proxy_no_cache $nocache;
# 添加缓存状态
add_header X-Cache-Status $upstream_cache_status;
proxy_pass $scheme://$http_host$request_uri;
proxy_set_header Host $http_host;
}
}
效果:
- 第一次访问 http://example.com 从目标服务器获取
- 后续相同请求直接从缓存返回,加速访问
实例五:多用户认证的正向代理
server {
listen 8888;
# 基础认证
auth_basic "Proxy Authentication Required";
auth_basic_user_file /etc/nginx/.proxy_passwd;
# 根据用户限速
limit_conn_zone $remote_addr zone=per_ip:10m;
location / {
resolver 8.8.8.8;
# 普通用户限速
if ($remote_user ~* "normal") {
set $limit_rate 100k;
}
# VIP 用户不限速
if ($remote_user ~* "vip") {
set $limit_rate 0;
}
limit_rate $limit_rate;
proxy_pass $scheme://$http_host$request_uri;
proxy_set_header Host $http_host;
proxy_set_header X-Proxy-User $remote_user;
}
}
创建密码文件:
# 安装 htpasswd 工具
yum install httpd-tools
# 创建用户
htpasswd -c /etc/nginx/.proxy_passwd normal_user
htpasswd /etc/nginx/.proxy_passwd vip_user
实例六:日志记录和分析
server {
listen 8888;
# 自定义日志格式,记录代理信息
log_format proxy '$remote_addr [$time_local] "$http_host" "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $upstream_cache_status';
access_log /var/log/nginx/proxy_access.log proxy;
error_log /var/log/nginx/proxy_error.log;
resolver 8.8.8.8;
location / {
proxy_pass $scheme://$http_host$request_uri;
proxy_set_header Host $http_host;
# 记录原始客户端 IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
客户端配置示例
Linux/Mac 命令行使用代理
# 临时使用
export http_proxy=http://proxy_ip:8888
export https_proxy=http://proxy_ip:8888
# 测试
curl -I http://example.com
Windows 浏览器配置
- 打开浏览器设置
- 找到代理设置
- 填写:地址 = 代理服务器IP,端口 = 8888
wget 使用代理
wget -e use_proxy=yes -e http_proxy=proxy_ip:8888 http://example.com/file
发表回复