# Configure Nginx With Multiple Locations With Different Root Folders on Subdomain

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`.
    
    ```bash
    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.
3. **Save and Close the File**
4. **Create a Symbolic Link**
    
    If you are using `/etc/nginx/sites-available/` and `/etc/nginx/sites-enabled/` for managing configurations, create a symbolic link:
    
    ```bash
    sudo ln -s /etc/nginx/sites-available/sub.example.com /etc/nginx/sites-enabled/
    ```
    
5. **Test Nginx Configuration**
    
    Before reloading Nginx, check the configuration for syntax errors:
    
    ```bash
    sudo nginx -t
    ```
    
6. **Reload Nginx**
    
    Apply the changes by reloading Nginx:
    
    ```bash
    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.