As web administrators, we often need to host multiple applications under the same domain using subdomains. Each application might require its own specific Nginx configuration. Today, we’ll explore how to implement different Nginx rules for your main domain and subdomains in DirectAdmin.
The Challenge
Consider this common scenario: You have WordPress running on your main domain, but you need to host a different application (like a calculator tool or an API) on a subdomain. Each requires its own unique Nginx configuration rules.
The Solution: Conditional Configuration Blocks
DirectAdmin provides an elegant way to handle this using conditional statements in your Nginx configuration. Here’s how to implement it:
- Navigate to Admin Level -> Custom Httpd Config in your DirectAdmin panel
- Select your domain
- Locate the nginx.conf configuration for… or CUSTOMX section
Implementation Example
Here’s a practical example of how to structure your configuration:
# Main domain WordPress configuration
|*if !SUB|
# WordPress specific rules
location = /favicon.ico {
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
|*endif|
# Subdomain specific configuration
|*if SUB="calculator"|
# Custom rules for calculator subdomain
location / {
# Your specific subdomain rules here
proxy_pass http://localhost:3000;
# Add other relevant configurations
}
|*endif|
Understanding DirectAdmin’s Nginx Conditionals
DirectAdmin provides powerful conditional statements for fine-tuning your Nginx configurations. Let’s break down the key conditional structures you can use:
Basic Conditional Syntax
|*if CONDITION|
# Your Nginx configuration here
|*endif|
Common Conditional Patterns
- Checking for Subdomains:
|*if !SUB|
# Main domain rules
|*endif|
|*if SUB="blog"|
# Blog subdomain specific rules
|*endif|
- Domain-Specific Rules:
|*if DOMAIN="example.com"|
# Rules for specific domain
|*endif|
- Combining Conditions:
|*if DOMAIN="example.com" && SUB="api"|
# Rules for api.example.com only
|*endif|
Pro Tips
- Use
!SUB
to target your main domain exclusively - Conditions are case-sensitive
- Multiple conditions can be combined using
&&
(AND) operator - Always test your conditional blocks before deploying
- Keep your conditions organized and well-commented
This conditional system allows you to create highly specific configurations while maintaining clean and manageable Nginx rules. It’s particularly useful when managing multiple applications or services across different subdomains of your website.
Remember that changes to these conditions require a web server reload to take effect, so plan your modifications accordingly during low-traffic periods.
Key Benefits
- Isolation: Each application gets its own specific configuration
- Maintainability: Clear separation between different site configurations
- Flexibility: Easy to add or modify rules for new subdomains
- Security: Better control over application-specific security requirements
Best Practices
- Always backup your configuration before making changes
- Test your configuration in a staging environment first
- Use meaningful subdomain names that reflect their purpose
- Document your custom configurations for future reference
- Keep security in mind when setting up specific rules
Important Notes
- Changes to Nginx configurations require a service restart
- Monitor your error logs after implementing changes
- Ensure your DNS records are properly configured for new subdomains
- Consider potential impact on SSL/TLS certificates
This solution provides a clean and maintainable way to manage different Nginx configurations for your various subdomains while keeping your main domain configuration intact.
Happy hosting!