NGINX: How to config domain subdirectory to point to a subfolder — Pavel Tashev

Pavel Tashev
2 min readOct 3, 2020

Description

Let’s say your website (e.g. mywebsite.com) is installed in the following directory on your server /var/www/mywebsite and you’d like to install a WordPress blog that can be accessed through the following URL mywebsite.com/blog.

You’ll have to configure NGINX to redirect users requests to a particular directory depending on the requested domain subfolder which in your case is mywebsite.com/blog.

There are two ways to do that.

The Subfolder Isn’t Inside Your Main Folder

The directory structure is the following.

/var/www/mywebsite 
/var/www/blog

Open your NGINX config file which should be located in /etc/nginx/sites-available/mywebsite and the following code (change it according to your needs).

location /blog { 
alias /var/www/blog;
try_files $uri $uri/ @blog;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
location @blog {
rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}

Check for errors and reload the NGING configurations.

nginx -t 
service nginx reload

The subfolder is inside the main folder

The directory structure is the following.

/var/www/mywebsite 
/var/www/mywebsite/blog

Open your NGINX config file and the following code (change it according to your needs).

location /blog { 
alias /var/www/mywebsite/blog;
try_files $uri $uri/ @blog;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
}
location @blog {
rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}

Check for errors and reload the NGING configurations.

nginx -t 
service nginx reload

If you need assistance contact me.

Originally published at https://www.paveltashev.com on October 3, 2020.

If you got any value from this, please give me a clap or two so others will see it too!

--

--

Pavel Tashev

Senior Full-Stack Developer at CampLight | CEO & Founder of DeepSource | www.paveltashev.com