Nginx 高级配置:打造高性能 Web 服务器
Nginx 是高性能 Web 服务器的代名词。本文深入探讨 Nginx 的高级配置,包括负载均衡策略、反向代理优化、缓存机制等,助你打造高可用的 Web 架构。
一、Nginx 架构概述
Nginx 采用事件驱动、异步非阻塞的架构,能够以极低的资源消耗处理大量并发连接。其核心特点包括:
Master-Worker 模型:一个主进程管理多个工作进程,充分利用多核 CPU。
事件模块:支持 epoll、kqueue 等高效事件模型。
模块化设计:核心功能简洁,通过模块扩展功能。
二、负载均衡配置
2.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;
location / {
proxy_pass http://backend;
proxy_set_header Host host;
proxy_set_header X-Real-IP remote_addr;
}
}
2.2 负载均衡策略
# 轮询(默认)
upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}
# 加权轮询
upstream backend {
server 192.168.1.10 weight=5;
server 192.168.1.11 weight=3;
}
# IP 哈希(会话保持)
upstream backend {
ip_hash;
server 192.168.1.10;
server 192.168.1.11;
}
# 最少连接
upstream backend {
least_conn;
server 192.168.1.10;
server 192.168.1.11;
}
# 一致性哈希
upstream backend {
hash request_uri consistent;
server 192.168.1.10;
server 192.168.1.11;
}
2.3 健康检查
upstream backend {
server 192.168.1.10 max_fails=3 fail_timeout=30s;
server 192.168.1.11 max_fails=3 fail_timeout=30s;
# 备用服务器
server 192.168.1.12 backup;
}
三、反向代理优化
3.1 代理缓冲配置
http {
# 代理缓冲
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
# 临时文件
proxy_temp_path /var/cache/nginx/proxy_temp;
proxy_max_temp_file_size 1024m;
}
3.2 连接优化
http {
# 保持连接
keepalive_timeout 65;
keepalive_requests 1000;
# 上游保持连接
upstream backend {
server 192.168.1.10;
keepalive 32;
}
server {
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://backend;
}
}
}
3.3 超时配置
http {
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
send_timeout 60s;
client_body_timeout 60s;
}
四、缓存配置
4.1 静态文件缓存
http {
# 缓存路径配置
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=my_cache:10m
max_size=1g inactive=60m;
server {
location / {
proxy_cache my_cache;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating;
proxy_cache_key schemeproxy_hostrequest_uri;
add_header X-Cache-Status upstream_cache_status;
proxy_pass http://backend;
}
}
}
4.2 浏览器缓存
server {
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
location ~* \.(html|htm)$ {
expires 1h;
add_header Cache-Control "public, must-revalidate";
}
}
五、SSL/TLS 优化
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
# SSL 优化
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
# 协议和加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
# HSTS
add_header Strict-Transport-Security "max-age=31536000" always;
}
六、限流与安全
6.1 请求限流
http {
limit_req_zone binary_remote_addr zone=req_limit:10m rate=10r/s;
limit_conn_zone binary_remote_addr zone=conn_limit:10m;
server {
location /api/ {
limit_req zone=req_limit burst=20 nodelay;
limit_conn conn_limit 10;
proxy_pass http://backend;
}
}
}
6.2 安全配置
server {
# 隐藏版本号
server_tokens off;
# 安全头
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
# 禁止访问敏感文件
location ~ /\. {
deny all;
}
location ~* \.(env|git|svn) {
deny all;
}
}
七、日志与监控
http {
# 自定义日志格式
log_format main 'remote_addr - remote_user [time_local] '
'"request" status body_bytes_sent '
'"http_referer" "http_user_agent" '
'request_time upstream_response_time';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
# 状态监控
server {
listen 8080;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
}
}
八、总结
Nginx 是 Web 服务器中的瑞士军刀,掌握其高级配置能让你构建出高性能、高可用的 Web 架构。
核心优化要点:
1. 合理配置负载均衡策略
2. 开启代理缓冲和缓存
3. 优化 SSL/TLS 性能
4. 配置限流保护后端
5. 添加安全头防止攻击
持续监控和调优是保持系统高性能的关键。
本文链接:https://www.kkkliao.cn/?id=776 转载需授权!
版权声明:本文由廖万里的博客发布,如需转载请注明出处。



手机流量卡
免费领卡
号卡合伙人
产品服务
关于本站
