惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
Security Latest
Security Latest
L
LINUX DO - 热门话题
Blog — PlanetScale
Blog — PlanetScale
T
Threatpost
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
S
Securelist
I
Intezer
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Heimdal Security Blog
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
The Register - Security
The Register - Security
NISL@THU
NISL@THU
S
Schneier on Security
M
MIT News - Artificial intelligence
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Y
Y Combinator Blog
Know Your Adversary
Know Your Adversary
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
美团技术团队
W
WeLiveSecurity
P
Privacy International News Feed
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
小众软件
小众软件
博客园 - 【当耐特】
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog

宏尘极客

halo-theme-dream2.0-plus 主题-新版朋友圈配置说明 halo-theme-dream2.0-plus 主题 1.20.2 版本-留言板配置说明 微信红包封面-马到功成 Windows11彻底关闭自动更新指南 WSL2 Docker 资源配置指南 绿联Pro 安装Dos游戏中文版webui,一个可以浏览器内玩dos游戏的容器 绿联Pro 安装 Stirling-PDF-V2 - 强大的本地托管基于 Web 的 PDF 操作工具 绿联Pro 部署在线游戏机 绿联Pro 安装OpenList一个支持多种存储的文件列表程序 绿联Pro 部署Memos - 轻量级自托管备忘录中心 绿联Pro 安装 Stirling-PDF - 强大的本地托管基于 Web 的 PDF 操作工具 绿联Pro 部署Upage - 基于大模型的可视化网页构建平台,Lovable 开源替代 不蒜子数据同步工具使用指南 绿联Pro 部署 PostgreSQL 数据库 绿联Pro 自建不蒜子统计服务,替代Dream2.0 Plus主题页脚统计数据 1panel使用编排模板部署不蒜子服务,自建不蒜子统计服务,替代Dream2.0 Plus主题页脚统计数据 绿联Pro 安装Photopea,轻量在线PS工具 绿联Pro 安装禅道(Zentao),一款功能强大、全面的敏捷项目管理软件 绿联Pro 安装MeterSphere,新一代的测试管理和接口测试工具 Docker Compose 标签介绍与示例指南 绿联Pro 安装Meilisearch 搜索引擎,为halo博客提供增强搜索引擎 解析 <meta> 标签的全局 Referrer 策略设置 电线粗细选择与家用大功率电器布线指南 Nginx 防盗链配置介绍 绿联Pro 部署meting-api服务并使用lukcy反向代理,自建音乐API服务 1panel使用编排模板部署meting-api服务,自建音乐API服务 java开发 - 通过客户端访问接口获取接口服务器实际部署地址 Git revert 撤销已推送到服务器的提交详解 绿联Pro 部署MediaGo,跨平台视频提取工具 C# 自定义事件与委托 绿联Pro 安装Umami,使用MySQL数据库,为你的网站添加网站监控
Nginx 反向代理简介
宏尘 · 2025-07-17 · via 宏尘极客

1. 基本反向代理配置

Nginx 反向代理的核心配置是通过 proxy_pass 指令将客户端请求转发至后端服务器。

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

关键参数说明:

  • proxy_pass:指定后端服务器地址(可以是 IP、域名或 upstream 组)。

  • proxy_set_header:修改请求头,确保后端服务器获取真实客户端 IP 等信息。


2. 负载均衡方案

Nginx 可以通过 upstream 模块实现多台后端服务器的负载均衡。

upstream backend {
    server 192.168.1.101:8080 weight=3;
    server 192.168.1.102:8080;
    server 192.168.1.103:8080 backup;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
    }
}

负载均衡策略:

  • 轮询(默认):请求按顺序分发到各服务器。

  • 加权轮询:通过 weight 参数分配不同权重。

  • IP 哈希:使用 ip_hash 确保同一客户端始终访问同一后端。

  • 最少连接:使用 least_conn 优先选择连接数最少的服务器。

  • 备份服务器:通过 backup 标记备用服务器,仅在主服务器不可用时启用。


3. HTTPS 反向代理

Nginx 可代理 HTTPS 请求,并支持 SSL 终止(在 Nginx 解密后转发明文至后端)。

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

关键配置:

  • ssl_certificatessl_certificate_key:指定 SSL 证书路径。

  • X-Forwarded-Proto:告知后端请求的原始协议(HTTP/HTTPS)。


4. 缓存加速

Nginx 可缓存后端响应,减少重复请求对后端服务器的压力。

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

server {
    location / {
        proxy_cache my_cache;
        proxy_pass http://backend_server;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
    }
}

缓存参数说明:

  • proxy_cache_path:定义缓存存储路径及内存区域大小。

  • proxy_cache_valid:设置不同状态码的缓存时间。


5. WebSocket 代理

Nginx 支持代理 WebSocket 连接,需额外配置头信息。

server {
    location /ws/ {
        proxy_pass http://backend_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

关键参数:

  • UpgradeConnection 头:确保 WebSocket 协议升级成功。


6. 安全加固

  • 限制访问:通过 allow/deny 控制 IP 访问。

  • 速率限制:使用 limit_req 防止 DDoS 攻击。

  • 隐藏头信息:移除 Server 等敏感头。

location / {
    proxy_pass http://backend_server;
    proxy_hide_header X-Powered-By;
    limit_req zone=req_limit burst=10;
}

总结

Nginx 反向代理功能强大,适用于负载均衡、HTTPS 代理、缓存加速等场景。合理配置可提升性能、安全性和可用性。