When working with Nginx on Debian/Ubuntu systems, the default configuration structure typically includes:
/etc/nginx/nginx.conf (main config)
/etc/nginx/conf.d/ (additional config files)
/etc/nginx/sites-available/ (virtual hosts)
/etc/nginx/modules-available/ (dynamic modules)
The fundamental issue arises because Nginx treats configuration directives as authoritative declarations rather than accumulative settings. Unlike Apache's configuration merging, Nginx will throw "duplicate directive" errors when the same directive appears in multiple included files.
1. Using include Directives Strategically
Create a new file in /etc/nginx/conf.d/override.conf
with:
# Override specific http context directives
http {
# New directives that don't exist in main config
client_max_body_size 50m;
# Alternative approach for existing directives
include /etc/nginx/conf.d/worker_settings.conf;
}
Then create worker_settings.conf
with:
worker_processes 4; # Override the auto setting
2. Environment-based Configuration
For more dynamic control, use environment variables:
worker_processes ${NGINX_WORKER_PROCESSES:-auto};
Then set the variable in your systemd service file or init script.
3. Configuration Generation
For complex scenarios, consider generating partial configurations:
#!/bin/bash
# generate_conf.sh
cat > /etc/nginx/conf.d/generated.conf <
To override SSL settings without touching the main config:
# /etc/nginx/conf.d/ssl_overrides.conf
http {
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
}
- Test configuration changes with
nginx -t
before reloading - The order of includes matters - later includes override earlier ones
- Some directives simply cannot be overridden (like daemon, pid)
- Consider using configuration management tools (Ansible, Puppet) for production systems
Use these commands to troubleshoot:
# Check which config files are loaded
nginx -T
# Verify configuration
nginx -t
# See the effective configuration (requires nginx-extras)
nginx -V
When working with Nginx on Debian systems, many developers face a dilemma: how to customize server settings while preserving the default /etc/nginx/nginx.conf
for easier future updates. Unlike other services that gracefully handle configuration overrides in conf.d/
, Nginx throws "duplicate directive" errors when attempting to modify existing http block settings.
The common pattern of adding custom configurations in /etc/nginx/conf.d/
works well for new server blocks or additional directives, but breaks when trying to override existing settings like:
worker_processes auto;
worker_connections 768;
keepalive_timeout 65;
Here's how to properly implement configuration overrides while maintaining the original nginx.conf:
# In your custom config file (/etc/nginx/conf.d/custom.conf)
http {
# Override specific directives
worker_processes 4;
worker_connections 1024;
# Include the original http context
include /path/to/original/http/context;
}
- First, extract the original http context from nginx.conf:
- Then create your override file:
- Modify the main nginx.conf to use your overrides:
grep -A1000 "^http {" /etc/nginx/nginx.conf > /etc/nginx/http_context.original
# /etc/nginx/conf.d/custom_overrides.conf
http {
# Custom overrides
sendfile off;
tcp_nopush off;
# Preserve original settings
include /etc/nginx/http_context.original;
}
# At the end of /etc/nginx/nginx.conf
include /etc/nginx/conf.d/custom_overrides.conf;
For more complex scenarios, you can use environment variables to control overrides:
http {
# Base configuration
include /etc/nginx/conf.d/base_config.conf;
# Environment-specific overrides
include /etc/nginx/conf.d/${NGX_ENV}_overrides.conf;
}
- Always test with
nginx -t
before reloading - Document your overrides clearly for future maintainers
- Consider version controlling your custom configurations
- Watch for directive conflicts during Nginx upgrades
For infrastructure-as-code environments, consider generating the final nginx.conf from a template:
# Using envsubst
envsubst < nginx.conf.template > /etc/nginx/nginx.conf
# Example template snippet
worker_processes ${NGINX_WORKER_PROCESSES:-auto};
worker_connections ${NGINX_WORKER_CONNECTIONS:-768};