You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress子目录多站点Nginx配置咨询与子站点首页访问异常排查

Hey there! Let's work through your WordPress subdirectory multisite Nginx issues—great news that posts are loading, so we just need to fix that homepage hangup for your sub-sites. Here's a complete breakdown of the correct config and how to resolve your specific problem:

Correct Nginx Configuration for WordPress Subdirectory Multisite

This config is tailored for your Debian + Nginx + PHP5-FPM setup, and handles all multisite requirements including sub-site media files and routing:

server {
    listen 80;
    server_name domain.tld;
    root /var/www/domain.tld; # Update this to your actual WordPress root path
    index index.php index.html index.htm;

    # Serve static files directly to reduce PHP load
    location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        expires 30d;
        access_log off;
    }

    # Handle sub-site media files stored in blogs.dir
    location ~ ^/wp-content/blogs.dir/ {
        internal;
        alias /var/www/domain.tld/wp-content/blogs.dir/; # Match your root path
        access_log off;
        expires 30d;
    }

    # Core routing: forward all unrecognized requests to WordPress
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # PHP-FPM processing with multisite-specific parameters
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock; # Adjust if your PHP-FPM uses a TCP port (e.g., 127.0.0.1:9000)
        fastcgi_index index.php;
        include fastcgi_params;

        # Critical for multisite: pass correct path info to WordPress
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

        # Security: only process existing PHP files
        try_files $uri =404;
    }

    # Block access to hidden files (e.g., .htaccess, .git)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}
Troubleshooting Sub-Site Homepage Access Issues

Since your sub-site posts work but homepages don't, the problem almost always relates to routing or multisite configuration. Follow these steps:

  1. Verify your wp-config.php settings
    Double-check that your multisite flags are set correctly for subdirectory mode—this is a common gotcha:

    define('WP_ALLOW_MULTISITE', true);
    define('MULTISITE', true);
    define('SUBDOMAIN_INSTALL', false); // MUST be false for subdirectory multisite
    define('DOMAIN_CURRENT_SITE', 'domain.tld');
    define('PATH_CURRENT_SITE', '/');
    define('SITE_ID_CURRENT_SITE', 1);
    define('BLOG_ID_CURRENT_SITE', 1);
    

    If SUBDOMAIN_INSTALL was set to true, that's definitely causing the homepage issue.

  2. Confirm Nginx routing rules
    The location / block's try_files $uri $uri/ /index.php?$args; is key. It tells Nginx to check for existing files/directories first, then forward everything else to WordPress. If your old config was missing $uri/, it wouldn't handle sub-site homepage requests (which are directory paths like /asite/).

  3. Check PHP-FPM PATH_INFO parameters
    The PATH_INFO and PATH_TRANSLATED parameters in the PHP block are essential for WordPress to parse sub-site paths. If your old config didn't include these, adding them will fix how WordPress receives routing data.

  4. Fix file permissions
    Ensure the wp-content/blogs.dir directory (where sub-site media is stored) is owned by your PHP-FPM user (usually www-data):

    chown -R www-data:www-data /var/www/domain.tld/wp-content/blogs.dir
    chmod -R 755 /var/www/domain.tld/wp-content/blogs.dir
    find /var/www/domain.tld/wp-content/blogs.dir -type f -exec chmod 644 {} \;
    
  5. Reload services
    After making changes, restart Nginx and PHP-FPM to apply them:

    service nginx reload
    service php5-fpm restart
    
  6. Cross-check with WordPress's .htaccess
    While Nginx doesn't use .htaccess, the auto-generated rules from WordPress can confirm your routing logic is correct. A subdirectory multisite .htaccess should look like this—your Nginx config mirrors this behavior:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
    RewriteRule ^(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]
    

After following these steps, your sub-site homepages should load correctly. If you still run into issues, check Nginx's error log (/var/log/nginx/error.log) for specific clues—often it'll point to permission issues or missing parameters.

内容的提问来源于stack exchange,提问作者fabsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:16:14