Nginx Default Public Www Location?

Better Stack Team
Updated on October 7, 2024

The default public web directory for Nginx is specified in its configuration file, and it can vary based on the operating system and how Nginx was installed. Here's how you can find and understand the default public directory:

1. Default Location on Common Linux Distributions

Ubuntu/Debian:

  • Default Directory: /var/www/html
  • Configuration File: The default configuration file is usually found at /etc/nginx/sites-available/default.

    Example Configuration:

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

CentOS/RedHat:

  • Default Directory: /usr/share/nginx/html
  • Configuration File: The default configuration file is usually found at /etc/nginx/nginx.conf or in /etc/nginx/conf.d/.

    Example Configuration:

     
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        root /usr/share/nginx/html;
        index index.html index.htm;
    
        server_name _;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    
    

2. How to Find the Default Directory

  1. Check Nginx Configuration File:

    • Open the default server configuration file. For most distributions, you can check the default configuration files:

       
      sudo cat /etc/nginx/sites-available/default
      

      or

       
      sudo cat /etc/nginx/nginx.conf
      
 
- Look for the `root` directive in the `server` block to find the default public directory.
  1. Check Active Configuration:

    • To ensure you're viewing the active configuration, you might want to list all the available server blocks:

       
      sudo ls /etc/nginx/sites-available/
      
 
- Check if there are any symbolic links to `sites-enabled` that might indicate active configurations:

    ```bash
    sudo ls -l /etc/nginx/sites-enabled/
    ```

3. Change the Default Directory

If you want to change the default document root to a different directory, you can modify the root directive in the Nginx configuration file.

Example Modification:

  1. Open the Configuration File:

     
    sudo nano /etc/nginx/sites-available/default
    

    or

     
    sudo nano /etc/nginx/nginx.conf
    
  2. Change the root Directive:

    • Update the root directive to point to your desired directory. For example:

       
      server {
          listen 80;
          listen [::]:80;
      
          root /path/to/your/new/public/directory;
          index index.html index.htm;
      
          server_name example.com;
      
          location / {
              try_files $uri $uri/ =404;
          }
      }
      
      
  3. Test the Configuration:

     
    sudo nginx -t
    

    This command checks the configuration for syntax errors.

  4. Reload Nginx:

     
    sudo systemctl reload nginx
    

    This applies the configuration changes without stopping the Nginx service.

Summary

  • Default Public Directory:
    • Ubuntu/Debian: /var/www/html
    • CentOS/RedHat: /usr/share/nginx/html
  • Find the Directory: Check the root directive in the default Nginx configuration files, usually located in /etc/nginx/.
  • Change the Directory: Modify the root directive in the Nginx configuration file and reload Nginx to apply changes.

By understanding and configuring the default public directory, you can effectively manage and serve your website’s static files using Nginx.

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.