针对2核2G内存的低配服务器的宝塔面板的Nginx优化设置 - 技术宅银魂 - 科技改变生活 - 万事屋 | 生活·动漫·娱乐综合社区-银魂同好聚集地

针对2核2G内存的低配服务器的宝塔面板的Nginx优化设置

最近一台服务器出老是出故障,运行缓慢,IO也慢,发现Nginx在其中扮演重要角色功不可没,负优化了属于是。现在那就来正优化下,解决以下问题:

  1. 调整worker_processes为auto,让Nginx自动根据CPU核心数设置
  2. 调整worker_rlimit_nofile为15000(2G内存建议值)
  3. 调整events中worker_connections为8192(2G内存建议值)
  4. 调整http块中的各种缓冲区大小,避免过大占用内存
  5. 调整超时时间,加快资源释放
  6. 调整open_file_cache大小,减少内存占用
  7. 调整FastCGI缓冲区大小和数量,减少内存占用
  8. 调整Gzip压缩级别为2,减少CPU消耗
  9. 设置连接限制和请求限制,防止资源耗尽
  10. 调整日志缓冲,减少磁盘IO
  11. 为PHPMyAdmin服务器禁用访问日志,减少IO

以下是nginx.conf完整代码:

user www www;
worker_processes auto;
worker_rlimit_nofile 15000;
error_log /www/wwwlogs/nginx_error.log warn;
pid /www/server/nginx/logs/nginx.pid;

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

http {
    include       mime.types;
    include       proxy.conf;
    default_type  application/octet-stream;

    # 基础优化 - 内存敏感配置
    server_names_hash_bucket_size 128;
    client_header_buffer_size 8k;
    large_client_header_buffers 4 8k;
    client_max_body_size 25m;

    # 超时设置 - 加快资源释放
    client_header_timeout 10s;
    client_body_timeout 10s;
    send_timeout 10s;

    # 连接优化 - 平衡并发与内存
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20s;
    keepalive_requests 50;
    reset_timedout_connection on;

    # 静态文件缓存 - 减少IO
    open_file_cache max=5000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;

    # FastCGI 优化 - PHP处理
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 120s;
    fastcgi_read_timeout 120s;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_intercept_errors on;

    # 压缩优化 - 低CPU消耗
    gzip on;
    gzip_min_length 1024;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;
    gzip_vary on;
    gzip_proxied any;
    gzip_disable "msie6";

    # 安全头 - 基础防护
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # 连接限制 - 防止资源耗尽
    limit_conn_zone $binary_remote_addr zone=perip:5m;
    limit_conn_zone $server_name zone=perserver:5m;
    limit_conn perip 30;
    limit_conn perserver 100;
    limit_req_zone $binary_remote_addr zone=reqlimit:5m rate=30r/s;
    limit_req_status 429;

    # 日志优化 - 减少磁盘IO
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                   '"$http_user_agent"';
    access_log /www/wwwlogs/access.log main buffer=16k flush=60s;
    error_log /www/wwwlogs/error.log;
    server_tokens off;

    # PHPMyAdmin 服务器 - 精简版
    server {
        listen 888;
        server_name phpmyadmin;
        index index.php;
        root /www/server/phpmyadmin;
        access_log off;
        error_log /www/wwwlogs/phpmyadmin.error.log;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            include enable-php.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~ /\.ht {
            deny all;
        }
        
        # 静态资源缓存
        location ~* \.(jpg|jpeg|gif|png|ico|css|js)$ {
            expires 30d;
            access_log off;
        }
    }

    # 包含所有虚拟主机配置
    include /www/server/panel/vhost/nginx/*.conf;
}

2核2GB专属优化说明:

  1. 内存优化

    • worker_connections 8192 → 限制并发连接数防止OOM

    • fastcgi_buffers 4 64k → 减少PHP进程内存占用

    • open_file_cache max=5000 → 控制文件缓存内存使用

  2. CPU优化

    • gzip_comp_level 2 → 降低压缩强度节省CPU

    • keepalive_requests 50 → 减少长连接处理开销

    • 禁用Brotli压缩 → 避免高CPU消耗

  3. 连接管理

    • limit_conn perip 30 → 防止单个IP耗尽连接

    • limit_conn perserver 100 → 全局限流保护

    • limit_req zone=reqlimit:5m rate=30r/s → 防止CC攻击

  4. IO优化

    • 访问日志缓冲:buffer=16k flush=60s

    • PHPMyAdmin访问日志禁用

    • 静态资源缓存减少磁盘访问

请登录后发表评论

    没有回复内容

万事屋新帖