NGINX: How to config domain subdirectory to point to a subfolder

How To Config Domain Subdirectory To Point To A Subfolder

NGINX: How to config domain subdirectory to point to a subfolder

Oct 3, 2020 – 2 min read

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.

Tags:
,
No Comments

Post A Comment