Server on virtualmin keeps redirecting to wrong website com WP multisite

When I enabled MultiSite Wordpress, it redirected to one of the Virualmin sites

I tried to put it as a subdomain, I registered it as DNS. As a subdirectory I tried to include it in the NGINX settings.

######### subdirectory #######

 location / {
            index index.php index.html index.htm;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }
location /layout-1/ {
            index index.php;
            try_files $uri $uri/ /layout-1/index.php?q=$uri&$args;
    }    
         
location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
  ########## use this or this depends on the configuration ######
1 - ###     fastcgi_pass unix:/run/php/php7.3-fpm.sock;
2 - ###  fastcgi_pass localhost:8009;
########################################################
       fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_read_timeout 3000;

;

Should the BIND have an external or internal IP in the domain? I use only one IP for all servers, and in BIND all domains are with external IP. (The question is whether it should be external or internal IP).

Would NGINX have any configuration? How to remove the IP and just put (listen IP: 80) instead of (listen 288.218.198.981:80)

But which configuration would work in general? So you can always avoid complex edits … For example … After creating an internal subdomain …

When enabling WordPress Multisite, configuration issues often arise, particularly when mixing subdirectories, subdomains, or DNS settings. Below are steps to ensure your setup works properly with NGINX, BIND, and WordPress Multisite:

1. DNS Configuration: Internal vs. External IP

  • External IP: If users from the internet need access, BIND should point to the external IP of your server.
  • Internal IP: Use this only if the server is accessed strictly within a private network.

For WordPress Multisite:

  • For subdomain-based multisite, each subdomain (e.g., sub1.example.com) must have a DNS A or CNAME record pointing to the external IP of your server.
  • For subdirectory-based multisite, DNS settings remain the same as the primary domain (e.g., example.com).

2. NGINX Configuration for WordPress Multisite

The primary NGINX configuration should handle both subdomains and subdirectories:

Base Configuration:

server {
    listen 80;
    server_name example.com *.example.com;

    root /var/www/html;
    index index.php index.html index.htm;

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

    location ~ ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock; # Adjust PHP socket or port
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|otf|ttc|webp|avif|mp4|webm|ogg|ogv|pdf|doc|docx|ppt|pptx|xls|xlsx|zip|rar|7z|tar|gz|bz2|txt|xml|rss|atom|json|vtt|srt)$ {
        expires max;
        log_not_found off;
    }
}

Specific for Subdirectory-based Multisite:

location /subdirectory/ {
    index index.php;
    try_files $uri $uri/ /subdirectory/index.php?q=$uri&$args;
}

3. Updating BIND for Subdomains

  1. Add A records for each subdomain. For wildcard subdomains:
*.example.com.  IN  A  288.218.198.981  # Replace with your external IP
  1. Reload BIND:
sudo systemctl reload named

4. Generalizing NGINX to Avoid Frequent Edits

You can create a generic configuration for subdomains or subdirectories:

For Subdomains:

server {
    listen 80;
    server_name ~^(?<subdomain>.+)\.example\.com$;

    root /var/www/html/$subdomain;
    index index.php index.html index.htm;

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

For Subdirectories:

Modify /etc/nginx/sites-available/example.com to:

server {
    listen 80;
    server_name example.com;

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

    location ~ ^/([_0-9a-zA-Z-]+/) {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
}

5. Changing NGINX Listen Directive

Replace:

listen 288.218.198.981:80;

With:

listen 80;

If you have multiple IPs and want NGINX to listen on all available IPs, this configuration works universally.

6. WordPress Multisite Setup

  • For Subdomain Multisite:
    1. In the WordPress admin, go to Tools > Network Setup.
    2. Select Subdomains and install the network.
  • For Subdirectory Multisite:
    1. Use the subdirectory-based configuration and ensure your primary NGINX block matches the DocumentRoot.

7. Debugging Common Issues

Multisite Redirects to Another Site

  • Ensure wp-config.php contains the correct multisite configuration:
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true); // Set to 'false' for subdirectory
define('DOMAIN_CURRENT_SITE', 'example.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);
  • Remove browser cache to clear old redirects.
  • Update the NGINX cache (if applicable):
sudo nginx -s reload

Subdomains Not Resolving

  • Check DNS settings and verify wildcard A record is present.
  • Ensure your server has access to handle requests for all subdomains.

Subdirectories Returning 404 Errors

  • Confirm .htaccess or NGINX rules for pretty permalinks are correctly configured:
sudo systemctl reload nginx

8. Testing

After applying these changes:

  • Use curl or a browser to test subdomains and subdirectories.
  • Check logs for troubleshooting:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Let me know if any issues persist!