当前位置: 首页 > news >正文

Nginx 部署前端项目、负载均衡与反向代理

基础配置详解:Nginx 部署前端项目、负载均衡与反向代理


一、部署前端项目(静态资源)
# 基本配置:托管静态资源
server
{listen 9529;server_name 139.xxx.82.1xx;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/dist;location /api {proxy_pass http://xxx.196.xxx.169:xxx;rewrite "^/api/(.*)$" /$1 break;#nginx跟后端服务器连接超时时间(代理连接超时)proxy_connect_timeout   18000;#后端服务器数据回传时间(代理发送超时)proxy_send_timeout      18000;#连接成功后,后端服务器响应时间(代理接收超时)proxy_read_timeout      18000;}    access_log  /www/wwwlogs/139.196.82.169.log;error_log  /www/wwwlogs/139.196.82.169.error.log;
}

二、反向代理(对接后端 API/服务)
server {listen 80;server_name api.example.com;location / {# 后端服务地址(可替换为实际地址)proxy_pass http://localhost:3000;# 关键请求头转发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_set_header X-Forwarded-Proto $scheme;# HTTP 1.1支持proxy_http_version 1.1;# 连接超时控制proxy_connect_timeout 5s;proxy_send_timeout 10s;proxy_read_timeout 30s;}
}
高级功能配置1. WebSocket 代理
location /ws/ {proxy_pass http://backend_ws;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";# 保持连接活跃proxy_set_header Keep-Alive "timeout=300, max=1000";
}2.路径重写
location /service/ {# 移除URL中的/service前缀再转发rewrite ^/service/(.*)$ /$1 break;proxy_pass http://backend-api;
}

三、负载均衡(多后端服务器分发流量)
# 定义后端服务器组
upstream backend-servers {# 基础轮询(默认)server 192.168.1.101:8080 weight=3;  # 权重3(处理更多请求)server 192.168.1.102:8080;# 可选负载策略:# least_conn;   # 最少连接数# ip_hash;      # 相同IP固定到同一后端
}server {listen 80;server_name loadbalance.example.com;location / {proxy_pass http://backend-servers;   # 指向upstream组proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 失败重试机制proxy_next_upstream error timeout http_500;proxy_connect_timeout 2s;           # 连接超时proxy_read_timeout 5s;              # 读取响应超时}
}

关键配置说明

  1. 静态资源部署

    • root:指定项目根目录路径
    • try_files:解决SPA路由刷新404问题
    • expires:设置静态资源缓存时间
  2. 反向代理核心指令

    • proxy_pass:转发请求到目标服务
    • proxy_set_header:透传客户端信息(如真实IP)
    • proxy_http_version 1.1 + Upgrade:WebSocket必配
  3. 负载均衡策略

    • 轮询 (round-robin):默认(可加权 weight
    • IP哈希 (ip_hash):会话保持
    • 最少连接 (least_conn):动态分配
    • 健康检查:自动屏蔽故障节点(需Nginx Plus或开源替代方案)
  4. 超时控制

    proxy_connect_timeout 3s; # 连接后端超时
    proxy_send_timeout 5s;    # 发送请求超时
    proxy_read_timeout 10s;   # 等待响应超时
    

5.关键配置说明

proxy_pass - 核心指令,定义后端服务地址proxy_set_header - 转发请求头信息Host $host:保持原始主机头
X-Real-IP:传递客户端真实IP
X-Forwarded-For:记录请求路径
连接超时控制proxy_connect_timeout:连接建立超时
proxy_send_timeout:请求发送超时
proxy_read_timeout:响应接收超时
WebSocket特殊配置Upgrade $http_upgrade
Connection "upgrade"
路径重写rewrite 修改URL路径后转发
break 标志停止后续rewrite处理

实战技巧

  1. 多项目部署:使用 server_name 区分不同域名
  2. HTTPS支持
    listen 443 ssl;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/privkey.pem;
    
  3. 跨域处理(CORS):
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    
  4. 流量控制
    location /downloads/ {limit_rate 500k;  # 限速500KB/s
    }
    

**附:可根据实际情况修改配置

http://www.dtcms.com/a/321275.html

相关文章:

  • Seaborn 学习笔记
  • DigitalProductId解密算法php版
  • 「安全发」ISV对接支付宝+小猎系统
  • Prometheus 通过读取文件中的配置来监控目标
  • [ MySQL 数据库 ] 环境安装配置和使用
  • Rocky Linux 安装 Google Chrome 浏览器
  • (附源码)基于SpringBoot的高校爱心捐助平台的设计与实现
  • USB (Universal Serial Bus,通用串行总线)
  • K次取反后最大化的数组和
  • [案例十] NX二次开发批量替换组件功能(装配环境)
  • 【Open3D】基础操作之三维数据结构的高效组织和管理
  • 【FreeRTOS】任务间通讯3:互斥量- Mutex
  • ctrl+alt+方向键导致屏幕旋转的解决方法
  • 基于双块轻量级神经网络的无人机拍摄的风力涡轮机图像去雾方法
  • No time to train! Training-Free Reference-Based Instance Segmentation之论文阅读
  • 机场风云:AI 云厂商的暗战,广告大战一触即发
  • 【实战】Dify从0到100进阶--中药科普助手(2)
  • 用browse实现菜单功能的方法
  • 快速上手 Ollama:强大的开源语言模型框架
  • Docker的安装使用以及常见的网络问题
  • 数据库恢复技术:保障数据安全的关键
  • DeepSeek辅助编写的带缓存检查的数据库查询缓存系统
  • Odoo 18 → Odoo 19 功能改动对比表
  • 基于Web的交互式坐标系变换矩阵计算工具
  • 时间复杂度计算(以for循环为例)
  • BBH详解:面向大模型的高阶推理评估基准与数据集分析
  • 轻松实现浏览器自动化——AI浏览器自动化框架Stagehand
  • 力扣 hot100 Day69
  • 使用 PicGo 与 GitHub 搭建高效图床,并结合 Local Images Plus 备份原图
  • 杂谈 001 · VScode / Copilot 25.08 更新