子目录与子域名两种多站点模式,在 Nginx 伪静态规则上有何差异?

解读

国内生产环境普遍使用 Nginx + PHP-FPM 架构,面试官想知道你是否能把 WordPress Multisite 的两种形态(子目录 / 子域名)与 Nginx 的 location 匹配、rewrite 阶段、server_name 通配三件事对应起来。核心考点有三:

  1. 子目录模式只有 一个 server 块,靠路径前缀区分站点;子域名模式靠 server_name 通配 把流量切到同一套代码。
  2. 子目录需要把 ms-files.php 的旧文件路由规则写进通用 location,子域名则不需要(默认已禁用 ms-files.php)。
  3. 国内往往要接 CDN + HTTPS + WAF,规则必须同时兼容「真实客户端协议」与「回源协议」字段,否则会出现重定向死循环或 404。

知识点

  • WordPress Multisite 常量 SUBDOMAIN_INSTALL 的布尔值决定两种模式。
  • Nginx 的 rewrite 阶段 在 location 匹配之前执行,因此先 server_name 再路径。
  • 子目录模式必须捕获 /blogname/yyyy/mm/slug/ 这种结构并交给 index.php 处理,同时把 /files/ 重写到 ms-files.php。
  • 子域名模式只需在 server_name 里写 *.example.com,然后所有站点共用一份 root,无需再区分路径。
  • 国内云厂商(阿里云、腾讯云、华为云)的 负载均衡回源 会把真实协议放在 X-Forwarded-Proto,规则里必须加 map http_x_forwarded_proto scheme 防止 https 被误判成 http。
  • 若站点开 「安全加固」插件(如 Wordfence),子目录模式要注意 location ~ .php$ 的优先级,避免插件生成的伪静态规则被覆盖。

答案

子目录模式:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    # 国内 CDN 回源兼容
    map $http_x_forwarded_proto $real_scheme {
        default $scheme;
        https   https;
    }

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

    # 多站点主规则
    rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ permanent;
    if (!-e $request_filename) {
        rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 last;
        rewrite ^/([_0-9a-zA-Z-]+/)?(.*)$ /index.php?$2 last;
    }

    # 旧版 ms-files.php 文件服务(国内仍有不少老站)
    location ~ ^/([_0-9a-zA-Z-]+/)?files/(.+) {
        try_files /wp-content/blogs.dir/1/files/$2 /wp-includes/ms-files.php?file=$2;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS $real_scheme if_not_empty;
    }
}

子域名模式:

server {
    listen 80;
    server_name *.example.com;
    root /var/www/html;

    map $http_x_forwarded_proto $real_scheme {
        default $scheme;
        https   https;
    }

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

    # 子域名模式无需捕获目录前缀,规则更短
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php last;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS $real_scheme if_not_empty;
    }
}

差异总结:

  1. server_name 子目录用 精确域名,子域名用 通配符。
  2. 子目录需要 两层 rewrite 把站点目录剥掉再交给 WordPress,子域名 一层即可。
  3. 子目录必须保留 ms-files.php 兼容规则,子域名 默认关闭,规则里可省。
  4. 国内 HTTPS 强制跳转要放在 map 之后,否则云负载均衡回源会造成 301 死循环。

拓展思考

  • 如果企业把 主站放在 example.com,分站用 city.example.com,但又要做 「城市子目录」兼容旧 SEO 权重(即 /shanghai/ 跳转到 shanghai.example.com),可在子目录规则里加一条 return 301 https://1.example.com1.example.comrequest_uri,实现 301 集中权重,同时保持 Nginx 配置单文件维护。
  • 当站点数 >500 时,通配符证书(*.example.com)的 TLS 握手性能 会成为瓶颈,可改用 SAN 证书 + 独立 server_name 哈希表,利用 Nginx 的 hash bucket 调优,减少内存抖动。
  • 国内公安备案要求 关闭未备案域名访问,可在子域名模式的 server 块最前面加
    if ($host !~ ^(www|beian|shanghai)\.example\.com$) { return 444; }
    
    直接拒绝未备案子域名,避免被监管部门扫描到空主机头。