When working with Nginx, you'll encounter three crucial directories that handle different aspects of server configuration:
/etc/nginx/ ├── conf.d/ # Modular configuration files ├── sites-available/ # All possible site configurations └── sites-enabled/ # Activated site configurations (symlinks)
The conf.d directory is designed for global server configurations and modules. Files here are typically included with an include
directive in nginx.conf:
# Example conf.d/load-balancing.conf upstream backend { server 10.0.0.1:8080; server 10.0.0.2:8080; }
The sites-available directory stores complete virtual host configurations. This is where you define server blocks for individual websites:
# Example sites-available/example.com server { listen 80; server_name example.com; location / { proxy_pass http://backend; } }
Nginx only reads configurations from sites-enabled. This directory typically contains symlinks to files in sites-available:
# Enable a site ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ # Disable a site rm /etc/nginx/sites-enabled/example.com
The www-data
user is typically created automatically during Nginx installation. To verify:
id www-data # Should return: uid=33(www-data) gid=33(www-data) groups=33(www-data)
For proper permissions:
# Set ownership for web content chown -R www-data:www-data /var/www/html # Set secure permissions find /var/www/html -type d -exec chmod 755 {} \; find /var/www/html -type f -exec chmod 644 {} \;
Here's a complete example for load balancing:
# /etc/nginx/conf.d/backend.conf upstream app_cluster { least_conn; server 192.168.1.10:8000; server 192.168.1.11:8000; keepalive 32; } # /etc/nginx/sites-available/myapp server { listen 80; server_name app.example.com; location / { proxy_pass http://app_cluster; proxy_http_version 1.1; proxy_set_header Connection ""; } }
Remember to test your configuration after changes:
nginx -t && systemctl reload nginx
When working with Nginx, you'll encounter three critical directories that handle server configurations differently. Understanding their purposes and best practices is essential for proper server management.
The conf.d
directory is Nginx's primary configuration location. Files here should end with .conf
and are automatically loaded when Nginx starts. This is ideal for:
# Example: /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com;
root /var/www/example;
}
This pair works together for configuration management:
- sites-available: Stores all possible configurations
- sites-enabled: Contains symlinks to active configurations
Enable a site with:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Directory | Purpose | Best For |
---|---|---|
conf.d | Automatic loading | Simple setups, global configs |
sites-available | Configuration storage | Complex multi-site setups |
sites-enabled | Active configurations | Selective site activation |
Nginx typically runs as www-data
:
# Check if user exists
id www-data
# Set permissions
chown -R www-data:www-data /var/www
chmod -R 755 /var/www
Here's a basic load balancing setup in conf.d
:
upstream backend {
server 10.0.0.1:8080;
server 10.0.0.2:8080;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
Always test before applying changes:
nginx -t
systemctl reload nginx