Bobcares

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.comCopy Code
  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;
    }
    }
    Copy Code

    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/Copy Code

Step 3: Test the Nginx Configuration

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

sudo nginx -tCopy Code

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 nginxCopy Code

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
    Copy Code
  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 *

Speed issues driving customers away?
We’ve got your back!