How to Change Nginx Root Directory Path on Ubuntu: Complete Configuration Guide


2 views

The root directive in Nginx specifies the base directory for serving files. When properly configured, Nginx will prepend the request URI to this root path to locate files. The issue you're experiencing typically stems from one of several common configuration pitfalls.

First, verify your entire configuration file structure. The most reliable location for server blocks is in /etc/nginx/sites-available/ with symlinks in /etc/nginx/sites-enabled/:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    
    root /var/www;
    index index.html index.htm;
    
    server_name _;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

After modifying your configuration, always perform these actions:

  1. Test configuration syntax: sudo nginx -t
  2. Reload Nginx: sudo systemctl reload nginx
  3. Check process status: sudo systemctl status nginx

File permissions often cause root directory issues. Ensure:

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

For a more advanced setup with multiple sites:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.html;
    
    location / {
        try_files $uri $uri/ =404;
    }
}

server {
    listen 80;
    server_name test.com;
    root /var/www/test.com;
    index index.html;
}

On systems with SELinux enabled, you may need to adjust file contexts:

sudo chcon -Rt httpd_sys_content_t /var/www
sudo restorecon -Rv /var/www

To confirm your changes took effect:

curl -I http://localhost
sudo grep -r "root" /etc/nginx/

The root directive in Nginx specifies the base directory for serving files. By default, Nginx on Ubuntu uses /usr/share/nginx/html, but you might want to change this to a custom directory like /var/www for better organization or project requirements.

Many users encounter issues when trying to change the root directory, often due to:

  • Syntax errors in the configuration file (missing semicolons, incorrect paths)
  • Insufficient permissions on the target directory
  • Not properly reloading or restarting Nginx after changes

Here's how to properly change the root directory in Nginx:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # Correct root directive with semicolon
    root /var/www;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }
}

After editing your configuration file:

  1. Test the configuration syntax: sudo nginx -t
  2. If successful, reload Nginx: sudo systemctl reload nginx
  3. For major changes, restart instead: sudo systemctl restart nginx

Ensure Nginx has proper access to your new root directory:

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

If changes don't take effect:

  • Check for multiple server blocks that might override your settings
  • Verify there are no syntax errors in other parts of the configuration
  • Examine Nginx error logs: sudo tail -f /var/log/nginx/error.log

For a more complex setup with multiple sites:

server {
    listen 80;
    server_name example.com;
    
    root /var/www/example.com;
    index index.php index.html;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}