核心原则

在 2 核 CPU、512MB 内存的极限约束下,服务器架构遵循三大原则:

  1. 零数据库:不运行 MySQL/PostgreSQL/Redis,所有数据以静态文件形式存在
  2. 零运行时渲染:不在服务器上执行 PHP/Python/Node.js,HTML 预编译
  3. 零动态请求:每个请求直接返回静态文件,无需计算

这三条原则确保服务器在 512MB 内存下仍能保持极高性能。

Nginx 极限调优

基础配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# /etc/nginx/nginx.conf - 2核512M专用配置
worker_processes 2;          # 匹配CPU核心数
worker_rlimit_nofile 1024;   # 文件描述符上限

events {
    worker_connections 512;   # 单worker最大并发连接
    use epoll;                # Linux高性能事件模型
    multi_accept on;          # 一次性接受所有新连接
}

http {
    # 连接优化
    sendfile on;              # 零拷贝传输
    tcp_nopush on;            # 优化数据包发送
    tcp_nodelay on;           # 禁用Nagle算法
    keepalive_timeout 30;     # 长连接超时
    keepalive_requests 1000;  # 单连接最大请求数

    # 缓冲区优化 - 小内存关键配置
    client_body_buffer_size 1K;
    client_header_buffer_size 1K;
    client_max_body_size 2m;
    large_client_header_buffers 2 1K;

    # 压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 256;
    gzip_types text/plain text/css application/json
               application/javascript text/xml
               application/xml text/markdown;

    # Brotli预压缩(如果安装了ngx_brotli模块)
    brotli_static on;
    brotli on;
    brotli_types text/plain text/css application/json
                 application/javascript text/xml
                 application/xml text/markdown;

    # 连接限制
    limit_conn_zone $binary_remote_addr zone=connlimit:1m;
    limit_conn connlimit 20;

    # 速率限制
    limit_req_zone $binary_remote_addr zone=ratelimit:1m rate=10r/s;
    limit_req zone=ratelimit burst=20 nodelay;
}

站点配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
server {
    listen 80;
    server_name airef.dev;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name airef.dev;

    # SSL - 最小内存配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:1m;     # 1MB共享缓存
    ssl_session_timeout 1h;
    ssl_session_tickets off;

    # 根目录
    root /var/www/airef.dev/public;
    index index.html;

    # AI发现文件 - 直接返回
    location = /llms.txt {
        default_type text/plain;
        add_header Cache-Control "public, max-age=3600";
    }

    location = /llms-full.txt {
        default_type text/plain;
        add_header Cache-Control "public, max-age=3600";
    }

    location = /robots.txt {
        default_type text/plain;
        add_header Cache-Control "public, max-age=86400";
    }

    location = /sitemap.xml {
        default_type application/xml;
        add_header Cache-Control "public, max-age=3600";
    }

    # 静态文件 - 强缓存
    location ~* \.(html|css|js|svg|png|jpg|webp|woff2)$ {
        expires 7d;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # 其他请求 - 兜底
    location / {
        try_files $uri $uri/ $uri.html =404;
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options DENY;
    }
}

内存管理

内存预算分配

组件 分配内存 说明
Linux 系统 150MB 含内核、基础服务
Nginx 50MB 2 worker × 25MB
SSH 10MB 远程管理
Swap 文件 1GB 安全缓冲
可用 ~300MB 预留给突发

Swap 配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 创建1GB swap文件
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# 调整swappiness - 尽量少用swap
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

# 永久挂载
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

禁用不必要的服务

1
2
3
4
5
6
7
8
# 查看内存占用
ps aux --sort=-%mem | head -20

# 禁用不需要的服务(根据实际情况调整)
sudo systemctl disable bluetooth
sudo systemctl disable cups
sudo systemctl disable avahi-daemon
sudo systemctl disable ModemManager

性能基准

在 2 核 512MB 服务器上的预期性能指标:

指标 目标值 实测值
首次字节时间 (TTFB) < 50ms ~15ms
页面加载时间 < 500ms ~200ms
并发连接数 100+ 200+
内存占用 (Nginx) < 80MB ~45MB
CPU 占用 (空闲) < 2% ~0.5%
吞吐量 1000 req/s 2500+ req/s

总结

2 核 512MB 服务器完全能够支撑一个高流量的静态技术参考站点。核心策略是:

  • 使用 Nginx 的最小化配置匹配硬件约束
  • 所有编译工作在 CI/CD 环境完成
  • 服务器只负责文件传输,不承担任何计算
  • Brotli/Gzip 压缩减少传输体积
  • 合理的缓存策略减少重复请求