以下是对Nginx反向代理服务器的HTTP协议支持和性能优化的详细技术解读:
多版本协议支持
nginx
listen 443 ssl http2; # 启用HTTP/2
listen 443 quic reuseport; # 实验性HTTP/3
协议升级机制
nginx
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
头部处理优化
nginx
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_request_headers on;
nginx
proxy_hide_header Server;
连接管理优化
keepalive_timeout 75s;
keepalive_requests 1000;
proxy_http_version 1.1;
proxy_set_header Connection "";
缓冲与缓存机制
nginx
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
nginx
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m inactive=60m;
proxy_cache mycache;
负载均衡算法
upstream backend {
least_conn;
server backend1.example.com weight=3;
server backend2.example.com;
keepalive 32;
}
TCP栈优化
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
零拷贝技术
sendfile on;
tcp_nopush on;
事件模型选择
use epoll; # Linux系统
worker_connections 10240;
multi_accept on;
协议安全限制
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
请求限制
limit_req_zone $binary_remote_addr zone=reqlimit:10m rate=10r/s;
状态监控
location /nginx_status {
stub_status on;
access_log off;
}
日志优化
log_format proxy_log '$remote_addr - $upstream_addr [$time_local] '
'"$request" $status $body_bytes_sent';
access_log /var/log/nginx/proxy.log proxy_log buffer=32k flush=5m;
events {
worker_connections 4096;
use epoll;
}
http {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=proxy_cache:10m;
upstream backend {
zone backend 64k;
least_conn;
server 10.0.1.101:8080 max_fails=3;
server 10.0.1.102:8080 backup;
}
server {
listen 80 proxy_protocol;
listen 443 ssl http2;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_cache proxy_cache;
proxy_cache_valid 200 302 10m;
}
}
}
建议根据实际业务场景进行压力测试,使用wrk
或jmeter
工具验证配置效果。