WordPress Installs in Root and Subfolder With nginx

We’ve been hosting various WordPress installs on the same server, as well as subdomain and subfolder multisites on nginx, but I had to fix a new subsite on a WordPress install that included another WordPress instance as a subfolder.

The scenario was related to a large WordPress setup with bbPress that had to be split up for performance and security reasons, including a redesign of the main site. The forums install was left untouched in terms of UX, the main site was drastically improved – with a new theme and hundreds of thousands of post entries down thanks to bbPress install moved to /forums instead. After a benchmark and a code/database review we noticed tens of thousands of transients autoloaded on each requests (forum entries available for edits stored as transients) which also improved the load time by getting rid of those on the main site.

I spent some time trying to enable the forums/ subfolder and resolve it as a separate site, but ended up with various nginx errors, such as:

rewrite or internal redirection cycle while internally redirecting to “/index.php

FastCGI sent in stderr: “Primary script unknown”

I played with different try_files rules, moving the index and root in and out of locations, and testing different location rules as well. I switched the php fastcgi params rules for php files, but no luck.

Turned out that server_name could be a subfolder, so a possible solution is simply creating two separate nginx conf files in sites-available/ symlinked to sites-enabled/ with two different servers for each of the sites.

Parent site:

[bash]
server {
listen 80;
server_name maindomain.com;

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

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

rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}

if ($uri !~ wp-content/plugins) {
rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*.php)$ $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*.php)$ $1 last;
}

location ~ .php$ {
try_files $uri /index.php;
autoindex on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_pass_header Set-Cookie;
include fastcgi_params;
}
}
[/bash]

Subfolder (the second instance with bbPress):

[bash]
server {
listen 80;
server_name maindomain.com/forums;

root /var/www/maindomain.com/forums;
index index.php index.html index.htm;

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

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}

if ($uri !~ wp-content/plugins) {
rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}

if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*.php)$ $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*.php)$ $1 last;
}

location ~ .php$ {
try_files $uri /index.php;
autoindex on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_pass_header Set-Cookie;
include fastcgi_params;
}
}
[/bash]

One thought on ““WordPress Installs in Root and Subfolder With nginx”

  1. Nemanja says: April 30, 2018 at 11:58 pm

    nginx: [warn] server name “domain.com/test” has suspicious symbols in…
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

Leave a Reply

Your email address will not be published. Required fields are marked *