By jockebq
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!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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:
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!
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?
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.