Comprehensive Guide to Nginx Multi-Subdomain Configuration: From Common Mistakes to Best Practices

Dec 11, 2025 · Programming · 11 views · 7.8

Keywords: Nginx configuration | subdomain setup | server block

Abstract: This article provides an in-depth exploration of configuring multiple subdomains in Nginx, focusing on the common error of nested server blocks often encountered by beginners. By comparing the configuration logic differences between Apache and Nginx, it systematically explains the correct usage of the server_name directive and provides complete configuration examples. The article also discusses practical techniques such as log separation and root directory setup, helping readers master efficient strategies for managing multiple subdomains.

Nginx Configuration Architecture and Server Block Fundamentals

Nginx, as a high-performance web server and reverse proxy, employs a modular configuration structure. Unlike Apache's virtual host configuration, Nginx uses independent server blocks to define different server configurations. Each server block represents a virtual server, with the server_name directive specifying the domain names that the server responds to.

Analysis of Common Configuration Errors

In the provided Q&A data, when attempting to add a second subdomain in the same configuration file, the user incorrectly nested one server block inside another. This nested structure is not permitted in Nginx configuration syntax and will cause configuration parsing to fail. The correct approach is to use independent server blocks to define each subdomain.

Example of incorrect configuration:

server {
    server_name www.example.com example.com;
    # Main configuration...
    
    server {  # Error: Nested server block
        server_name sub1.example.com;
        # Subdomain configuration...
    }
}

Correct Configuration Method

Based on the guidance from the best answer, the correct configuration should create independent server blocks for each subdomain:

# Main domain configuration
server {
    server_name example.com www.example.com;
    access_log /srv/www/www.example.com/logs/access.log;
    error_log /srv/www/www.example.com/logs/error.log;
    root /srv/www/www.example.com/public_html;
    
    location / {
        index index.html index.htm;
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
    }
}

# First subdomain configuration
server {
    server_name sub1.example.com;
    access_log /srv/www/example.com/logs/sub1-access.log;
    error_log /srv/www/example.com/logs/sub1-error.log;
    root /srv/www/example.com/sub1;
    
    location / {
        index index.html index.htm;
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/sub1$fastcgi_script_name;
    }
}

# Second subdomain configuration
server {
    server_name sub2.example.com;
    access_log /srv/www/example.com/logs/sub2-access.log;
    error_log /srv/www/example.com/logs/sub2-error.log;
    root /srv/www/example.com/sub2;
    
    location / {
        index index.html index.htm;
    }
    
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/sub2$fastcgi_script_name;
    }
}

Configuration Key Points Analysis

1. server_name Directive: This directive supports wildcards and regular expressions, but it is recommended to use full domain names for explicit subdomains. Multiple domain names can be separated by spaces.

2. Log Separation: Configure independent access logs and error logs for each subdomain to facilitate troubleshooting and traffic analysis.

3. Root Directory Setup: Each subdomain should have an independent document root directory to avoid file conflicts.

4. PHP Processing: Ensure that fastcgi_param SCRIPT_FILENAME points to the correct subdomain directory.

Configuration Verification and Reload

After completing the configuration, use the following command to verify the configuration syntax:

nginx -t

If it displays "syntax is ok", you can reload the configuration using:

nginx -s reload

Advanced Configuration Recommendations

For scenarios with a large number of subdomains, consider using wildcard configurations or map-based configuration optimizations. Additionally, it is recommended to split the configurations of different subdomains into separate files and manage them uniformly through the include directive to improve configuration maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.