Bobcares

How to Configure Nginx for Multiple Locations with Different Root on a Subdomain

by | Mar 19, 2025

Learn how to configure Nginx with multiple locations with different roots on a subdomain. Our Nginx Support team is here to help you with your questions and concerns.

How to Configure Nginx with Multiple Locations

How to Configure Nginx with Multiple LocationsSometimes, we may need to serve different root folders for different locations when setting up a subdomain in Nginx.

This is a common requirement for websites that host various sections under a single subdomain. Fortunately, Nginx lets us do this by defining multiple location blocks within a server block.

This blog will take you through configuring Nginx to handle different locations under a subdomain, each pointing to a unique root folder.

Step 1: Define the Server Block

To begin, we need to define a server block for the subdomain in our Nginx configuration file. This block determines how requests to the subdomain are handled.

Here is an example:

Suppose we are configuring the subdomain `sub.example.com` with different root folders for specific locations:

  • `/location1` should serve content from `/var/www/subdomain/location1`
  • `/location2` should serve content from `/var/www/subdomain/location2`

Let’s take a look at how to set it up:

  1. In most cases, configuration files are stored in `/etc/nginx/sites-available/` or `/etc/nginx/conf.d/`. In this example, we’ll use `/etc/nginx/sites-available/sub.example.com`:

    sudo nano /etc/nginx/sites-available/sub.example.com

  2. Then, add the Server Block


    server {
    listen 80;
    server_name sub.example.com;
    # Root folder for the main location
    root /var/www/subdomain;
    # Default location
    location / {
    try_files $uri $uri/ =404;
    }
    # Location for /location1
    location /location1 {
    alias /var/www/subdomain/location1;
    try_files $uri $uri/ =404;
    }
    # Location for /location2
    location /location2 {
    alias /var/www/subdomain/location2;
    try_files $uri $uri/ =404;
    }
    # Optional: Location for PHP files (if using PHP)
    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    # Optional: Location for static files like images, CSS, JS
    location ~* \.(jpg|jpeg|png|gif|css|js)$ {
    expires 30d;
    access_log off;
    }
    }

    Here,

    • `root /var/www/subdomain;`: Specifies the root folder for the default location (`/`).
    • `alias /var/www/subdomain/location1;`: Defines the root folder for `/location1`. The `alias` directive is used instead of `root` because it serves files directly from the specified path.
    • `alias /var/www/subdomain/location2;`: Defines the root folder for `/location2` similarly.

Step 2: Save and Enable the Configuration

After adding the server block, save the file and close the text editor.

If we are using `/etc/nginx/sites-available/` and `/etc/nginx/sites-enabled/` for managing configurations, create a symbolic link:

sudo ln -s /etc/nginx/sites-available/sub.example.com /etc/nginx/sites-enabled/

Step 3: Test the Nginx Configuration

Before reloading Nginx, verify that the configuration file has no syntax errors:

sudo nginx -t

If the test is successful, we will see an output indicating that the syntax is correct.

Step 4: Reload Nginx

To apply the changes, reload Nginx:

sudo systemctl reload nginx

Additional Tips

  1. First, make sure that the Nginx user (usually `www-data`) has the appropriate permissions to read files in the specified directories:


    sudo chown -R www-data:www-data /var/www/subdomain
    sudo chmod -R 755 /var/www/subdomain

  2. Also, confirm that the directory structure exists and matches what is defined in the configuration.
  3. If we serve dynamic content such as PHP files, ensure that PHP-FPM is correctly installed and running. If you are using a different PHP version, modify the `fastcgi_pass` directive.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

By following the above steps, we can easily configure Nginx to serve different root folders for multiple locations on a subdomain, ensuring efficient content management and accessibility.

In brief, our Support Experts demonstrated how to troubleshoot the “nfs-subdir-external-provisioner not found” error in Kubernetes.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF