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
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.
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:
Open the Configuration File:
sudo nano /etc/nginx/sites-available/default
or
sudo nano /etc/nginx/nginx.conf
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; } }
Test the Configuration:
sudo nginx -t
This command checks the configuration for syntax errors.
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
- Ubuntu/Debian:
- 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.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github