企业级 Nginx 反向代理配置通常涉及多个方面,包括负载均衡、SSL/TLS 加密、缓存、日志记录等。以下是一个基本的企业级 Nginx 反向代理配置示例:
1. 基本配置
| 定义上游服务器 |
| upstream backend { |
| server 192.168.1.10:8080; |
| server 192.168.1.11:8080; |
| server 192.168.1.12:8080; |
| } |
| server { listen 80; server_name example.com; |
| |
| return 301 https://$host$request_uri; |
| } |
| server { listen 443 ssl; server_name example.com; |
| |
| ssl_certificate /etc/nginx/ssl/example.com.crt; |
| ssl_certificate_key /etc/nginx/ssl/example.com.key; |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers HIGH:!aNULL:!MD5; |
| |
| |
| access_log /var/log/nginx/access.log; |
| error_log /var/log/nginx/error.log; |
| |
| |
| location / { |
| proxy_pass http://backend; |
| 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; |
| } |
| |
| |
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { |
| expires 30d; |
| add_header Cache-Control "public, no-transform"; |
| } |
| |
| |
| error_page 404 /404.html; |
| location = /404.html { |
| internal; |
| } |
| |
| |
| location = /health { |
| access_log off; |
| return 200 "healthy\n"; |
| } |
| } |
2. 解释
- upstream:定义后端服务器组,实现负载均衡。
- listen 80:监听 HTTP 请求,并重定向到 HTTPS。
- listen 443 ssl:监听 HTTPS 请求。
- ssl_certificate 和 ssl_certificate_key:指定 SSL 证书和私钥的路径。
- ssl_protocols 和 ssl_ciphers:配置支持的 SSL 协议和加密套件。
- access_log 和 error_log:配置访问日志和错误日志的路径。
- location /:处理所有请求,反向代理到后端服务器。
- proxy_set_header:设置转发请求头,传递客户端信息。
- location ~ .(jpg|jpeg|png|gif|ico|css|js)$*:配置静态文件缓存。
- error_page:配置自定义错误页面。
- location = /health:配置健康检查接口。
3. 高级配置
- 会话持久性:使用
ip_hash
实现会话持久性。
| upstream backend { |
| ip_hash; |
| server 192.168.1.10:8080; |
| server 192.168.1.11:8080; |
| server 192.168.1.12:8080; |
| } |
| limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; |
| server { |
| ... location / { |
| limit_req zone=one burst=5 nodelay; |
| proxy_pass http://backend; ... |
| } |
| } |
| proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri"; |
| server { |
| ... location / { |
| proxy_cache my_cache; |
| proxy_cache_valid 200 301 302 10m; |
| proxy_cache_valid 404 1m; |
| proxy_pass http://backend; ... |
| } |
| } |
4. 测试和部署
| sudo systemctl reload nginx |
二,nginx 负载均衡多个项目 企业级配置
在企业级环境中,Nginx 作为负载均衡器可以同时管理多个项目。以下是一个示例配置,展示了如何为多个项目配置负载均衡,包括 SSL/TLS 加密、会话持久性、健康检查等功能。
1. 基本配置
假设我们有两个项目:project1 和 project2,每个项目有多个后端服务器。
| |
| upstream project1_backend { |
| server 192.168.1.10:8080; |
| server 192.168.1.11:8080; |
| server 192.168.1.12:8080; |
| } |
| |
| |
| upstream project2_backend { |
| server 192.168.1.20:8080; |
| server 192.168.1.21:8080; |
| server 192.168.1.22:8080; |
| } |
| |
| |
| server { |
| listen 80; |
| server_name project1.example.com project2.example.com; |
| |
| return 301 https://$host$request_uri; |
| } |
| |
| |
| server { |
| listen 443 ssl; |
| server_name project1.example.com; |
| |
| ssl_certificate /etc/nginx/ssl/project1.example.com.crt; |
| ssl_certificate_key /etc/nginx/ssl/project1.example.com.key; |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers HIGH:!aNULL:!MD5; |
| |
| access_log /var/log/nginx/project1.access.log; |
| error_log /var/log/nginx/project1.error.log; |
| |
| location / { |
| proxy_pass http://project1_backend; |
| 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; |
| } |
| |
| |
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { |
| expires 30d; |
| add_header Cache-Control "public, no-transform"; |
| } |
| |
| |
| location = /health { |
| access_log off; |
| return 200 "healthy\n"; |
| } |
| } |
| |
| |
| server { |
| listen 443 ssl; |
| server_name project2.example.com; |
| |
| ssl_certificate /etc/nginx/ssl/project2.example.com.crt; |
| ssl_certificate_key /etc/nginx/ssl/project2.example.com.key; |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers HIGH:!aNULL:!MD5; |
| |
| access_log /var/log/nginx/project2.access.log; |
| error_log /var/log/nginx/project2.error.log; |
| |
| location / { |
| proxy_pass http://project2_backend; |
| 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; |
| } |
| |
| |
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { |
| expires 30d; |
| add_header Cache-Control "public, no-transform"; |
| } |
| |
| |
| location = /health { |
| access_log off; |
| return 200 "healthy\n"; |
| } |
| } |
| |
2. 解释
- upstream:定义每个项目的后端服务器组,实现负载均衡。
- listen 80:监听 HTTP 请求,并重定向到 HTTPS。
- listen 443 ssl:监听 HTTPS 请求。
- ssl_certificate 和 ssl_certificate_key:指定 SSL 证书和私钥的路径。
- ssl_protocols 和 ssl_ciphers:配置支持的 SSL 协议和加密套件。
- access_log 和 error_log:配置访问日志和错误日志的路径。
- location /:处理所有请求,反向代理到后端服务器。
- proxy_set_header:设置转发请求头,传递客户端信息。
- *location ~ .(jpg|jpeg|png|gif|ico|css|js)$**:配置静态文件缓存。
- location = /health:配置健康检查接口。
3. 高级配置
会话持久性
使用 ip_hash 实现会话持久性,确保同一个客户端的请求总是被转发到同一台后端服务器。
| upstream project1_backend { |
| ip_hash; |
| server 192.168.1.10:8080; |
| server 192.168.1.11:8080; |
| server 192.168.1.12:8080; |
| } |
| |
| upstream project2_backend { |
| ip_hash; |
| server 192.168.1.20:8080; |
| server 192.168.1.21:8080; |
| server 192.168.1.22:8080; |
| } |
| |
限流
限制每个 IP 的请求速率,防止滥用。
| limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; |
| |
| server { |
| ... |
| location / { |
| limit_req zone=one burst=5 nodelay; |
| proxy_pass http://project1_backend; |
| ... |
| } |
| } |
缓存
配置更复杂的缓存策略。
| proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m; |
| proxy_cache_key "$scheme$request_method$host$request_uri"; |
| |
| server { |
| ... |
| location / { |
| proxy_cache my_cache; |
| proxy_cache_valid 200 301 302 10m; |
| proxy_cache_valid 404 1m; |
| proxy_pass http: |
| ... |
| } |
| } |
| |
4. 测试和部署
| sudo systemctl reload nginx |
以上配置示例和解释可以帮助您构建一个基本的企业级 Nginx 负载均衡配置,管理多个项目。根据实际需求,您可以进一步调整和优化配置。
三,nginx 负载均衡怎么调比例
在 Nginx 中,可以通过配置 upstream 块中的 weight 参数来调整各个后端服务器的负载比例。weight 参数决定了每个后端服务器接收请求的权重,权重越高,该服务器接收到的请求越多。
以下是一个示例配置,展示了如何为不同的后端服务器设置不同的权重:
示例配置
假设我们有两个项目:project1 和 project2,每个项目有多个后端服务器,我们希望为这些服务器设置不同的负载比例。
| |
| upstream project1_backend { |
| server 192.168.1.10:8080 weight=3; |
| server 192.168.1.11:8080 weight=2; |
| server 192.168.1.12:8080 weight=1; |
| } |
| |
| |
| upstream project2_backend { |
| server 192.168.1.20:8080 weight=4; |
| server 192.168.1.21:8080 weight=1; |
| server 192.168.1.22:8080 weight=1; |
| } |
| |
| |
| server { |
| listen 80; |
| server_name project1.example.com project2.example.com; |
| |
| return 301 https://$host$request_uri; |
| } |
| |
| |
| server { |
| listen 443 ssl; |
| server_name project1.example.com; |
| |
| ssl_certificate /etc/nginx/ssl/project1.example.com.crt; |
| ssl_certificate_key /etc/nginx/ssl/project1.example.com.key; |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers HIGH:!aNULL:!MD5; |
| |
| access_log /var/log/nginx/project1.access.log; |
| error_log /var/log/nginx/project1.error.log; |
| |
| location / { |
| proxy_pass http://project1_backend; |
| 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; |
| } |
| |
| |
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { |
| expires 30d; |
| add_header Cache-Control "public, no-transform"; |
| } |
| |
| |
| location = /health { |
| access_log off; |
| return 200 "healthy\n"; |
| } |
| } |
| |
| |
| server { |
| listen 443 ssl; |
| server_name project2.example.com; |
| |
| ssl_certificate /etc/nginx/ssl/project2.example.com.crt; |
| ssl_certificate_key /etc/nginx/ssl/project2.example.com.key; |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers HIGH:!aNULL:!MD5; |
| |
| access_log /var/log/nginx/project2.access.log; |
| error_log /var/log/nginx/project2.error.log; |
| |
| location / { |
| proxy_pass http://project2_backend; |
| 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; |
| } |
| |
| |
| location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { |
| expires 30d; |
| add_header Cache-Control "public, no-transform"; |
| } |
| |
| |
| location = /health { |
| access_log off; |
| return 200 "healthy\n"; |
| } |
| } |
| |
解释
- upstream:定义每个项目的后端服务器组。
- weight:设置每个后端服务器的权重。权重值越高,该服务器接收到的请求越多。
- server 192.168.1.10:8080 weight=3; 表示 192.168.1.10:8080 的权重为 3。
- server 192.168.1.11:8080 weight=2; 表示 192.168.1.11:8080 的权重为 2。
- server 192.168.1.12:8080 weight=1; 表示 192.168.1.12:8080 的权重为 1。
- 其他配置选项
- max_fails:设置在失败后标记服务器为不可用之前的最大失败次数。
- fail_timeout:设置在标记服务器为不可用之后的超时时间。
- backup:将某个服务器标记为备用服务器,只有在其他服务器都不可用时才会使用。
例如:
| upstream project1_backend { |
| server 192.168.1.10:8080 weight=3 max_fails=3 fail_timeout=30s; |
| server 192.168.1.11:8080 weight=2 max_fails=3 fail_timeout=30s; |
| server 192.168.1.12:8080 weight=1 backup; |
| } |
测试和部署
| sudo systemctl reload nginx |
通过上述配置,您可以根据实际需求调整各个后端服务器的负载比例,确保资源的合理分配。
在 Nginx 的负载均衡配置中,weight 参数用于设置后端服务器的权重。权重决定了该服务器接收请求的概率。具体来说,权重越高,该服务器接收到的请求越多。
weight 参数的含义
- 默认值:如果未指定 weight,默认值为 1。
- 权重值:权重值是一个正整数,表示该服务器相对于其他服务器的重要性或处理能力。
示例解释
假设我们有以下 upstream 配置:
| upstream backend { |
| server 192.168.1.10:8080 weight=3; |
| server 192.168.1.11:8080 weight=2; |
| server 192.168.1.12:8080 weight=1; |
| } |
在这个配置中:
- 192.168.1.10:8080 的权重为 3。
- 192.168.1.11:8080 的权重为 2。
- 192.168.1.12:8080 的权重为 1。
负载分配计算
Nginx 会根据权重值的比例来分配请求。具体计算方法如下:
- 总权重:所有服务器的权重之和。
- 每个服务器的权重比例:
- 192.168.1.10:8080 的权重比例 = 3 / 6 = 0.5 (50%)
- 192.168.1.11:8080 的权重比例 = 2 / 6 ≈ 0.333 (33.33%)
- 192.168.1.12:8080 的权重比例 = 1 / 6 ≈ 0.167 (16.67%)
- 请求分配:
- 大约 50% 的请求会被分配给 192.168.1.10:8080。
- 大约 33.33% 的请求会被分配给 192.168.1.11:8080。
- 大约 16.67% 的请求会被分配给 192.168.1.12:8080。
- 使用场景
- 性能差异:如果某些服务器的性能更强,可以为其设置更高的权重,使其承担更多的请求。
- 资源分配:根据服务器的资源情况(如 CPU、内存、带宽等)调整权重,确保资源的合理利用。
- 测试和调试:在新服务器上线时,可以先设置较低的权重,逐步增加,观察其性能表现。
- 示例配置
| upstream project1_backend { |
| server 192.168.1.10:8080 weight=3; |
| server 192.168.1.11:8080 weight=2; |
| server 192.168.1.12:8080 weight=1; |
| } |
| |
| server { |
| listen 80; |
| server_name project1.example.com; |
| |
| location / { |
| proxy_pass http://project1_backend; |
| 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; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2023-11-13 Brew安装的MySQL8.0忘记密码修改密码