Configure Nginx With Multiple Locations With Different Root Folders on Subdomain

Better Stack Team
Updated on October 7, 2024

Configuring Nginx to serve different root folders for different locations on a subdomain is a common requirement. You can achieve this by defining multiple location blocks within a server block and specifying different root directives for each location.

Here’s a step-by-step guide on how to configure Nginx for a subdomain with multiple locations, each pointing to a different root folder:

1. Define the Server Block

Start by defining the server block for your subdomain in your Nginx configuration file. This block will handle requests for the subdomain.

Example Configuration

Assume you want to configure a subdomain sub.example.com, with different root folders for different locations:

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

Here’s how you can set it up:

  1. Open or Create the Nginx Configuration File

    Typically located in /etc/nginx/sites-available/ or /etc/nginx/conf.d/. For this example, let's use /etc/nginx/sites-available/sub.example.com.

     
    sudo nano /etc/nginx/sites-available/sub.example.com
    
  2. 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;
        }
    }
    
    
 
- **`root /var/www/subdomain;`**: Specifies the root folder for the default location (`/`).
- **`alias /var/www/subdomain/location1;`**: Sets the root folder for the `/location1` URI. Note that `alias` is used here instead of `root` because it allows for serving files under a different path directly.
- **`alias /var/www/subdomain/location2;`**: Similarly, sets the root folder for the `/location2` URI.
  1. Save and Close the File
  2. Create a Symbolic Link

    If you 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/
    
  3. Test Nginx Configuration

    Before reloading Nginx, check the configuration for syntax errors:

     
    sudo nginx -t
    
  4. Reload Nginx

    Apply the changes by reloading Nginx:

     
    sudo systemctl reload nginx
    

Additional Considerations

  • Permissions: Ensure that the Nginx user (usually www-data) has the correct permissions to read the directories and files.
  • Directory Structure: Make sure the directory structure on the filesystem matches what’s specified in the configuration.
  • Dynamic Content: If you are serving dynamic content (e.g., PHP files), ensure that the configuration for handling such files (like PHP-FPM) is correctly set up.

Summary

  • Define server block: Configure the server block for your subdomain.
  • Use alias directive: Specify different root folders for different locations using the alias directive.
  • Ensure proper permissions: Verify that Nginx can access the directories.
  • Test and reload Nginx: Apply changes by testing and reloading Nginx.

By following these steps, you can effectively configure Nginx to serve different root folders for various locations on a subdomain.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github