nginx.ccWeb 服务器排障台

多站点 Nginx 配置打包:一次生成多个站点的分文件配置

一次填写多个站点(WordPress、Node、静态、反向代理……),生成共用片段 + 每站一个文件的 Nginx 配置,打包成 tar.gz / zip,一条命令解压到 /etc/nginx 并 nginx -t。

← 单站点生成器 · 首次上线步骤

文件(10)

下载 .tar.gz 下载 .zip

/etc/nginx/conf.d/00-nginxcc.conf
# /etc/nginx/conf.d/00-nginxcc.conf — 所有站点共用的 http 层设置(nginx.cc 生成)

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

# upstream keepalive 需要清空 Connection 头
map $http_upgrade $connection_upgrade_keepalive {
    default upgrade;
    ''      '';
}

# gzip on 写在 nginx.conf(或各站点的 general.conf)里;Debian/Ubuntu 的 nginx.conf 已有 gzip on,这里重复会报 duplicate
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;
/etc/nginx/nginxcc/security.conf
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用:
# add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
/etc/nginx/nginxcc/general.conf
# 隐藏文件(.git、.env 等)
location ~ /\.(?!well-known) {
    deny all;
}

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;
/etc/nginx/nginxcc/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
    root /var/www/_letsencrypt;
}
/etc/nginx/nginxcc/ssl.conf
# Mozilla Intermediate(Mozilla Server Side TLS 6.0)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# 不开 OCSP stapling:Let's Encrypt 已于 2025 年停止 OCSP 服务。
/etc/nginx/nginxcc/proxy.conf
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
/etc/nginx/nginxcc/php_fastcgi.conf
try_files $fastcgi_script_name =404;   # 防止 /upload/x.jpg/a.php 被当成 PHP 执行
include fastcgi_params;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_hide_header X-Powered-By;
fastcgi_read_timeout 120s;
/etc/nginx/conf.d/blog.example.com.conf
# blog.example.com — 由 nginx.cc 生成 · WordPress
# 保存为 /etc/nginx/conf.d/blog.example.com.conf,然后 nginx -t && systemctl reload nginx

limit_req_zone $binary_remote_addr zone=blog_example_com_login:10m rate=10r/m;

server {
    listen 80;
    listen [::]:80;
    server_name blog.example.com;

    # Let's Encrypt 验证(证书续期也走这里)
    include nginxcc/letsencrypt.conf;

    location / {
        return 301 https://blog.example.com$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name blog.example.com;

    ssl_certificate     /etc/letsencrypt/live/blog.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/blog.example.com/privkey.pem;
    include nginxcc/ssl.conf;

    server_tokens off;
    client_max_body_size 64m;

    # 安全头
    include nginxcc/security.conf;
    add_header Strict-Transport-Security "max-age=63072000" always;

    access_log /var/log/nginx/blog.example.com.access.log;
    error_log  /var/log/nginx/blog.example.com.error.log warn;

    include nginxcc/general.conf;

    root /var/www/blog.example.com;
    index index.php index.html;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location = /robots.txt {
        access_log off;
        log_not_found off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;   # WordPress 固定链接
    }

    # WordPress:wp-includes 下的 PHP 不允许直接访问(TinyMCE 例外)
    location = /wp-includes/js/tinymce/wp-tinymce.php {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        include nginxcc/php_fastcgi.conf;
    }

    location ~* ^/wp-includes/.*\.php$ {
        deny all;
    }

    # WordPress:上传目录禁止执行脚本
    location ~* ^/wp-content/uploads/.*\.(?:php|phar|s?html?|swf)$ {
        deny all;
    }

    # WordPress:敏感文件
    location ~* ^/(?:xmlrpc\.php|wp-links-opml\.php|wp-config\.php|wp-config-sample\.php|readme\.html|license\.txt)$ {
        deny all;
    }

    # WordPress:登录限流(每 IP 每分钟 10 次)
    location = /wp-login.php {
        limit_req zone=blog_example_com_login burst=2 nodelay;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        include nginxcc/php_fastcgi.conf;
    }

    location ~* \.(?:css(?:\.map)?|js(?:\.map)?|mjs|jpe?g|png|gif|ico|cur|heic|webp|avif|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv|svgz?|ttf|ttc|otf|eot|woff2?)$ {
        expires 30d;
        access_log off;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        include nginxcc/php_fastcgi.conf;
    }
}
/etc/nginx/conf.d/app.example.com.conf
# app.example.com — 由 nginx.cc 生成 · Node.js
# 保存为 /etc/nginx/conf.d/app.example.com.conf,然后 nginx -t && systemctl reload nginx

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    # Let's Encrypt 验证(证书续期也走这里)
    include nginxcc/letsencrypt.conf;

    location / {
        return 301 https://app.example.com$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name app.example.com;

    ssl_certificate     /etc/letsencrypt/live/app.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
    include nginxcc/ssl.conf;

    server_tokens off;
    client_max_body_size 64m;

    # 安全头
    include nginxcc/security.conf;
    add_header Strict-Transport-Security "max-age=63072000" always;

    access_log /var/log/nginx/app.example.com.access.log;
    error_log  /var/log/nginx/app.example.com.error.log warn;

    include nginxcc/general.conf;

    # Next.js 的带 hash 静态文件可长期缓存(其他框架可删掉这段)
    location /_next/static/ {
        proxy_pass http://127.0.0.1:3000;
        include nginxcc/proxy.conf;
        expires 1y;
        access_log off;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        include nginxcc/proxy.conf;
    }
}
/etc/nginx/conf.d/www.example.org.conf
# www.example.org — 由 nginx.cc 生成 · 静态网站
# 保存为 /etc/nginx/conf.d/www.example.org.conf,然后 nginx -t && systemctl reload nginx

server {
    listen 80;
    listen [::]:80;
    server_name www.example.org;

    # Let's Encrypt 验证(证书续期也走这里)
    include nginxcc/letsencrypt.conf;

    location / {
        return 301 https://www.example.org$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name www.example.org;

    ssl_certificate     /etc/letsencrypt/live/www.example.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.org/privkey.pem;
    include nginxcc/ssl.conf;

    server_tokens off;
    client_max_body_size 64m;

    # 安全头
    include nginxcc/security.conf;
    add_header Strict-Transport-Security "max-age=63072000" always;

    access_log /var/log/nginx/www.example.org.access.log;
    error_log  /var/log/nginx/www.example.org.error.log warn;

    include nginxcc/general.conf;

    root /var/www/www.example.org;
    index index.html;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location = /robots.txt {
        access_log off;
        log_not_found off;
    }

    location / {
        try_files $uri $uri/ =404;
    }

    location ~* \.(?:css(?:\.map)?|js(?:\.map)?|mjs|jpe?g|png|gif|ico|cur|heic|webp|avif|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv|svgz?|ttf|ttc|otf|eot|woff2?)$ {
        expires 30d;
        access_log off;
    }
}

安装

  1. 还没有证书?先装 HTTP 版(阶段一)
    sudo mkdir -p /var/www/_letsencrypt
    curl -fsSL 'https://nginx.cc/api/gen/bundle.tar.gz?site=s%3Dwordpress%26domain%3Dblog.example.com%26https%3D0%26www%3Dnone&site=s%3Dnode%26domain%3Dapp.example.com%26https%3D0%26www%3Dnone%26upstream%3D127.0.0.1%253A3000&site=s%3Dstatic%26domain%3Dwww.example.org%26https%3D0%26www%3Dnone' | sudo tar -xzf - -C /etc/nginx
    sudo nginx -t && sudo systemctl reload nginx
  2. 签发证书
    sudo certbot certonly --webroot -w /var/www/_letsencrypt -d blog.example.com --cert-name blog.example.com --deploy-hook "systemctl reload nginx"
    sudo certbot certonly --webroot -w /var/www/_letsencrypt -d app.example.com --cert-name app.example.com --deploy-hook "systemctl reload nginx"
    sudo certbot certonly --webroot -w /var/www/_letsencrypt -d www.example.org --cert-name www.example.org --deploy-hook "systemctl reload nginx"
  3. 备份、解包、检查、重载
    sudo cp -a /etc/nginx /etc/nginx.bak.$(date +%Y%m%d-%H%M%S)
    curl -fsSL 'https://nginx.cc/api/gen/bundle.tar.gz?site=s%3Dwordpress%26domain%3Dblog.example.com%26https%3D1%26www%3Dnone&site=s%3Dnode%26domain%3Dapp.example.com%26https%3D1%26www%3Dnone%26upstream%3D127.0.0.1%253A3000&site=s%3Dstatic%26domain%3Dwww.example.org%26https%3D1%26www%3Dnone' | sudo tar -xzf - -C /etc/nginx
    sudo nginx -t && sudo systemctl reload nginx
    nginx -t 不通过就用备份还原:sudo rm -rf /etc/nginx && sudo mv /etc/nginx.bak.<时间> /etc/nginx

压缩包里的路径相对 /etc/nginx;nginxcc/README.txt 里有同样的命令。共用片段放在 /etc/nginx/nginxcc/,不会覆盖发行版自带文件(带 nginx.conf 时会覆盖 nginx.conf)。