NGINX Configuration For Multisite WordPress Setup – 2 Cases

Ubuntu Server, WordPress No Comments

Case 1: Normal WordPress setup in the root and multisite setup inside a directory in it.
For more clarity, https://domain.com => WordPress Single Site Setup
https://domain.com/demo => Demo is a folder (subdirectory) and has WordPress multisite setup.
For this case, below is the NGINX config file:

server {
 listen 443;
 server_name domain.com www.domain.com;
 root /var/www/html;
 index index.html index.htm index.php;
 ssl on;
 ssl_certificate /var/www/keys/namecheap/nginx_bundle_9ac0cec5ce44.crt;
 ssl_certificate_key /var/www/keys/domain.com.key;

 ssl_prefer_server_ciphers on;  
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

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

    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^/demo(/[^/]+)?(/wp-.*) /demo$2 last;      
        rewrite ^/demo(/[^/]+)?(/.*\.php)$ /demo$2 last;
    }

    location /demo/ {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /demo/index.php?$args;
    }

    location ~ \.(hh|php)$ {
        try_files $uri /demo/index.php;
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

server {
    listen 80;
    server_name domain.com; 
    return 301 https://$host$request_uri;
}

Case 2: Multisite WordPress setup in a subdomain co-hosted on the same server.
Let’s say the subdomain is subdom.yourdomain.com, for this the NGINX config file will look like this:

server {
 listen 443;
 server_name subdom.domain.com www.subdom.domain.com;
 root /var/www/subdom;
 index index.html index.htm index.php;
 ssl on;
 ssl_certificate /var/www/keys/subdom/namecheap/nginx_bundle_364dadc573b5.crt;
 ssl_certificate_key /var/www/keys/subdom/subdom.domain.com.key;

 ssl_prefer_server_ciphers on;  
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

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

    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;      
        rewrite ^(/[^/]+)?(/.*\.php)$ $2 last;
    }

    location ~ \.(hh|php)$ {
        try_files $uri /index.php;
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

server {
    listen 80;
    server_name subdom.domain.com; 
    return 301 https://$host$request_uri;
}

Hope this helps.

 

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments