← 返回首页
🌐

Web服务器性能优化

📂 devops ⏱ 2 min 293 words

Web服务器性能优化

Nginx优化

进程和连接

worker_processes auto;        # 自动匹配CPU核心
worker_rlimit_nofile 65535;   # 最大文件描述符

events {
    worker_connections 4096;  # 每个worker最大连接数
    use epoll;                # Linux高效IO模型
    multi_accept on;          # 一次接受多个连接
}

缓冲区配置

http {
    # 客户端缓冲
    client_body_buffer_size 16k;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    
    # 代理缓冲
    proxy_buffer_size 4k;
    proxy_buffers 8 4k;
    proxy_busy_buffers_size 8k;
}

Gzip压缩

http {
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
    gzip_min_length 1000;
}

缓存配置

# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

# 代理缓存
proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=1g;

location / {
    proxy_cache my_cache;
    proxy_cache_valid 200 10m;
    proxy_cache_valid 404 1m;
}

Apache优化

MPM配置

# /etc/apache2/mods-available/mpm_event.conf
<IfModule mpm_event_module>
    StartServers            2
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadsPerChild         25
    MaxRequestWorkers       400
    MaxConnectionsPerChild  1000
</IfModule>

KeepAlive配置

KeepAlive On
KeepAliveTimeout 5
MaxKeepAliveRequests 100

实践:Nginx高性能配置

# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /run/nginx.pid;

events {
    worker_connections 4096;
    use epoll;
    multi_accept on;
}

http {
    # 基础设置
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    
    # 日志格式
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log /var/log/nginx/access.log main buffer=16k;
    error_log /var/log/nginx/error.log warn;
    
    # Gzip
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml;
    
    # 连接优化
    open_file_cache max=10000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    
    include /etc/nginx/conf.d/*.conf;
}

HTTP/2优化

server {
    listen 443 ssl http2;
    
    # HTTP/2优化
    http2_max_concurrent_streams 128;
    http2_recv_buffer_size 32k;
}

SSL优化

server {
    listen 443 ssl http2;
    
    # SSL优化
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    
    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
}

性能测试

# Apache Bench
ab -n 10000 -c 100 http://localhost/

# wrk
wrk -t4 -c100 -d30s http://localhost/

# 压力测试脚本
#!/bin/bash
URL="http://localhost"
REQUESTS=10000
CONCURRENCY=100

echo "开始压力测试..."
wrk -t4 -c$CONCURRENCY -d30s $URL

监控和调优

# 查看Nginx连接状态
curl http://localhost/nginx_status

# 启用stub_status模块
location /nginx_status {
    stub_status on;
    allow 127.0.0.1;
    deny all;
}

总结

Web服务器性能优化需要从连接管理、缓冲配置、压缩、缓存等多个方面进行。通过合理的配置和持续监控,可以显著提升Web服务的性能。