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:
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; } }
Since your sub-site posts work but homepages don't, the problem almost always relates to routing or multisite configuration. Follow these steps:
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_INSTALLwas set totrue, that's definitely causing the homepage issue.Confirm Nginx routing rules
Thelocation /block'stry_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/).Check PHP-FPM PATH_INFO parameters
ThePATH_INFOandPATH_TRANSLATEDparameters 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.Fix file permissions
Ensure thewp-content/blogs.dirdirectory (where sub-site media is stored) is owned by your PHP-FPM user (usuallywww-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 {} \;Reload services
After making changes, restart Nginx and PHP-FPM to apply them:service nginx reload service php5-fpm restartCross-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.htaccessshould 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

