Nginx setup: Wordpress installation in root + Wordpress Multisite installation in subfolder?

October 2, 2019 79 views
WordPress Nginx

First off, I just setup a debian server with LEMP.
I have a very special setup, which is one Wordpress installation in the root folder, and a Wordpress Multisite network which is installed in a subfolder called “/u”.
The multisite network is served on subfolders too, not subdomains.

The current server is using Apache, which is setup this way.
Main (single site, in root):

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  Redirect /login /u/login
  Redirect /register /u/register
  Redirect /link /u/link
  Redirect /link-tv /u/link-tv
  Redirect /wp-login.php /u/wp-login.php
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Multisite (subdir network, install in “/u”)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /u/
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)postpass/?$ /u/wp-login.php?action=postpass [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)logout/?$ /u/wp-login.php?action=logout [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)lostpassword/?$ /u/wp-login.php?action=lostpassword [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)resetpassword/?$ /u/wp-login.php?action=resetpass [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)signup/?$ /u/wp-login.php?action=register [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)login/?$ /u/wp-login.php [QSA,L]
    RewriteRule ^(([_0-9a-zA-Z-]+/)?)dashboard/?$ /u/wp-login.php [QSA,L]
</IfModule>


RewriteEngine On
RewriteBase /u
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

This Apache setup works great, but how do I convert this to work on Nginx?

All help is appreciated!
Thanks!

3 Answers

Hi! As you can guess, your custom Apache config will take a little work to migrate to Nginx but it is 100% doable. Here’s an older tutorial with some good info to get started:

https://www.digitalocean.com/community/tutorials/how-to-migrate-from-an-apache-web-server-to-nginx-on-an-ubuntu-vps

Pay attention to the comments as there is some good info in there too, like this online converter for some of the rewrite rules:

https://winginx.com/en/htaccess

Hope this helps and if you get stuck please follow up in here and we’ll help you work through any issues if possible.

Good luck!

by Justin Ellingwood
Apache and Nginx are both powerful web servers that can reliably host web content on the internet. While Apache is the currently the most popular web server in the world, Nginx is rapidly gaining converts due to its low resource usage. In this guide, we will discuss how to migrate from Apache to Nginx.

Thank you!
I read the guide and this is how far I have got:

server {
    root /var/www/example.com;
    index index.php;

    server_name example.com www.example.com;

    include global/restrictions.conf;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    return 404; # managed by Certbot
}

And the restrictions.conf file:

# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
    log_not_found off;
    access_log off;
}

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
    deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

This works for the main site, I can now navigate my main single Wordpress install which is in the root folder.
However the Wordpress Multisite install which is located in folder “/u” is still broken.

I think I solved it.

This is what I got:

server {
    root /var/www/example.com;
    index index.php;

    server_name example.com www.example.com;

    include global/restrictions.conf;

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^/u(/[^/]+)?(/wp-.*) /u$2 last;
        rewrite ^/u(/[^/]+)?(/.*\.php)$ /u$2 last;
    }
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location /u/ {
        try_files $uri $uri/ /u/index.php?$args ;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar$
        access_log off;
        log_not_found off;
        expires max;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name example.com www.example.com;
    return 404; # managed by Certbot
}

Am I missing anything important?

  • Great work! Looks like you have everything covered with the main site config, the rewrites and https redirects. Of course, please test thoroughly and let us know how it goes :)

Have another answer? Share your knowledge.