How to Configure a Different Document Root for a Subdirectory in Nginx

Introduction

Nginx is a high-performance web server commonly used to serve websites and web applications.

In some cases, you may need to configure a different document root for a specific subdirectory while keeping the main domain’s document root unchanged.

For example:

  • Main domain document root: /var/www/html
  • Subdirectory document root: /var/www/folder

With this configuration, the main website can continue using /var/www/html, while requests to:

http://domain.tld/subdirectory/

are served from:

/var/www/folder

This guide explains how to configure this setup in Nginx.

Prerequisites

Before starting, make sure you have:

  1. An Nginx web server.
  2. SSH or terminal access to the server.
  3. Sufficient permissions to modify the Nginx configuration.
  4. Basic knowledge of Nginx configuration.

If Nginx is not installed, refer to the following installation guides:

Implementation

Step 1: Identify the Document Roots

For this example, assume the domain name is:

domain.tld

The main domain uses:

/var/www/html

The new directory that will be used for the subdirectory is:

/var/www/folder

The objective is to serve:

http://domain.tld/subdirectory/

from:

/var/www/folder

Step 2: Edit the Nginx Configuration

The default Nginx configuration file is:

/etc/nginx/sites-enabled/default

If your domain uses a different Nginx configuration file, update the corresponding configuration file instead.

Open the configuration file:

vi /etc/nginx/sites-enabled/default

Add the following configuration inside the server section:

location /subdirectory {

    alias /var/www/folder;

}

The alias directive maps the /subdirectory URL path to the /var/www/folder directory.

Step 3: Verify the Nginx Configuration

Before restarting Nginx, verify that the configuration syntax is correct:

nginx -t

If the configuration test is successful, proceed with restarting Nginx.

Step 4: Restart Nginx

Restart the Nginx service:

systemctl restart nginx

Step 5: Create Test Content

Create an index.html file inside the new document root:

vi /var/www/folder/index.html

Add some test content to the file and save it.

Step 6: Test the Subdirectory

Access the following URL from a browser:

http://domain.tld/subdirectory/index.html

The content from:

/var/www/folder/index.html

should now be displayed.

Conclusion

Nginx can be configured to serve a specific URL subdirectory from a different document root while keeping the main website’s document root unchanged.

In this example, /var/www/html continues to serve the main domain, while /var/www/folder serves requests under /subdirectory.

The key Nginx configuration is:

location /subdirectory {

    alias /var/www/folder;

}

This approach can be useful when different sections of a website need to be served from separate directories while using the same domain.

Frequently Asked Questions

1. Can a subdirectory use a different document root in Nginx?

Yes. Nginx’s alias directive can map a URL path to a different directory on the server.

2. Where should the location block be added?

The location block should be added inside the appropriate Nginx server block for the domain.

3. How can I verify the Nginx configuration before restarting?

Run:

nginx -t

This checks the Nginx configuration syntax before the service is restarted.

Talk to Our Technology Experts

Planning your AWS infrastructure or looking to improve an existing cloud environment? Our team can help with cloud architecture, infrastructure, DevOps, security, deployment, and ongoing optimization.

Connect with our technology experts.

admin

Writes about Web & Architecture at Pheonix Solutions.

Leave a Reply

Scroll to Top