Creating Custom Nginx Rules for Different Subdomains in DirectAdmin

0 comments 61 views 2 minutes read
Published on: October 27, 2024

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:

  1. Navigate to Admin Level -> Custom Httpd Config in your DirectAdmin panel
  2. Select your domain
  3. Locate the nginx.conf configuration for… or CUSTOMX section

Implementation Example

Here’s a practical example of how to structure your configuration:

Nginx
# 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

Nginx
|*if CONDITION|
    # Your Nginx configuration here
|*endif|

Common Conditional Patterns

  1. Checking for Subdomains:
Nginx
|*if !SUB|
    # Main domain rules
|*endif|

|*if SUB="blog"|
    # Blog subdomain specific rules
|*endif|
  1. Domain-Specific Rules:
Nginx
|*if DOMAIN="example.com"|
    # Rules for specific domain
|*endif|
  1. Combining Conditions:
Nginx
|*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

  1. Always backup your configuration before making changes
  2. Test your configuration in a staging environment first
  3. Use meaningful subdomain names that reflect their purpose
  4. Document your custom configurations for future reference
  5. 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!

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.