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

请求配置主站在根目录的WordPress多站点(子目录模式)NGINX环境

Hey there! I’ve configured plenty of subdirectory-based WordPress multisites on Nginx over the years, so I get exactly why you’re frustrated—most tutorials default to subdomain setups. Let’s walk through the exact config you need, plus key setup checks to make sure it works smoothly.

Nginx Configuration for WordPress Subdirectory Multisite

Here’s a complete, tested config tailored to your setup (main site at root, others at /site-name/):

server {
    listen 80;
    server_name www.example.com;
    root /var/www/example.com; # Replace with your actual WordPress root directory
    index index.php index.html index.htm;

    # Serve static files directly for better performance
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }

    # Core routing for main site and all subdirectory sites
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # Handle PHP requests with proper PATH_INFO support (critical for subdirectory sites)
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        # Update this to match your PHP-FPM setup (unix socket or TCP)
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        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;
    }

    # Block access to hidden files (like .htaccess, .git)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

Key Parts Explained

Let’s break down why each section matters:

  • Base Server Settings: server_name targets your main domain, root points to where your WordPress core files live (this is the same directory for all multisite sites).
  • Static File Handling: This skips PHP processing for assets like images and CSS, which speeds up your site and reduces server load.
  • Core Routing: The try_files rule ensures every request (whether for the main site or a subdirectory like /second-site/) gets routed to WordPress’s index.php if the file/directory doesn’t exist—this is how WordPress handles multisite routing internally.
  • PHP/PATH_INFO Handling: Subdirectory multisites rely on PATH_INFO to identify which site to load. The fastcgi_split_path_info line correctly splits the PHP script path from the subdirectory path, and we pass those values to PHP-FPM so WordPress can parse them.
  • Hidden File Protection: Blocks access to sensitive files that shouldn’t be public.

Critical Pre-Requisites

Before you apply this config, make sure your WordPress setup is correctly configured for subdirectory multisites:

  1. In your wp-config.php, add these lines (adjust values to match your domain):
    define('WP_ALLOW_MULTISITE', true);
    define('MULTISITE', true);
    define('SUBDOMAIN_INSTALL', false); // This is KEY—set to false for subdirectory mode
    define('DOMAIN_CURRENT_SITE', 'www.example.com');
    define('PATH_CURRENT_SITE', '/');
    define('SITE_ID_CURRENT_SITE', 1);
    define('BLOG_ID_CURRENT_SITE', 1);
    
  2. Ensure you’ve already run the WordPress multisite setup wizard (under Tools > Network Setup) and selected the subdirectory option.
  3. Update the fastcgi_pass line in the Nginx config to match your PHP version (e.g., php8.1-fpm.sock if you’re on PHP 8.1).

Final Steps

After saving the config:

  • Test your Nginx config for syntax errors: sudo nginx -t
  • If no errors, restart Nginx: sudo systemctl restart nginx
  • Restart your PHP-FPM service (if needed): sudo systemctl restart php8.2-fpm

If you hit 404s or other issues, check Nginx’s error log (/var/log/nginx/error.log) for clues—most problems are either file path mismatches or incorrect PHP-FPM settings.

Let me know if you need help troubleshooting any part of this!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 10:05:24