Files
hzdyjs/docs/部署文档/生产环境双机部署方案/配置文件/nginx-load-balance.conf
T
2025-11-27 00:02:01 +08:00

135 lines
3.7 KiB
Plaintext

# Nginx负载均衡配置
# 适用于双机部署场景
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'upstream: $upstream_addr response_time: $upstream_response_time';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
# 基础配置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 100M;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# 后端服务器池
upstream backend_servers {
# 机器67的后端实例
server 127.0.0.1:8080 weight=2 max_fails=3 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=3 fail_timeout=30s;
# 机器68的后端实例
server 172.16.0.68:8080 weight=2 max_fails=3 fail_timeout=30s;
server 172.16.0.68:8081 weight=1 max_fails=3 fail_timeout=30s;
# 健康检查
keepalive 32;
}
# Web前端服务器配置
server {
listen 80;
server_name your-domain.com;
# Web前端静态资源
location / {
root /root/hzdyjsweb9091/dist;
try_files $uri $uri/ /index.html;
expires 1y;
add_header Cache-Control "public, immutable";
}
# 移动端前端静态资源
location /mobile {
alias /root/mobile/dist;
try_files $uri $uri/ /mobile/index.html;
expires 1y;
add_header Cache-Control "public, immutable";
}
# API代理
location /prod-api/ {
proxy_pass http://backend_servers/;
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;
# 超时设置
proxy_connect_timeout 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# 缓冲设置
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 4k;
proxy_busy_buffers_size 8k;
# 错误处理
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
proxy_next_upstream_tries 3;
}
# 健康检查端点
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# 状态监控页面
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 172.16.0.0/16;
deny all;
}
}
# HTTPS配置(如需要)
# server {
# listen 443 ssl http2;
# server_name your-domain.com;
#
# ssl_certificate /etc/nginx/ssl/cert.pem;
# ssl_certificate_key /etc/nginx/ssl/key.pem;
# ssl_protocols TLSv1.2 TLSv1.3;
# ssl_ciphers HIGH:!aNULL:!MD5;
#
# # 其他配置同HTTP
# }
}