Nginx Domain Redirect Best Practices: Comprehensive Guide for www and non-www Configuration

Nov 19, 2025 · Programming · 11 views · 7.8

Keywords: Nginx | Domain_Redirection | www_Redirect | Server_Configuration | SEO_Optimization

Abstract: This article provides an in-depth analysis of www to non-www domain redirection in Nginx, examining common configuration errors and their solutions. Through comparison of multiple implementation approaches, it explains the advantages of using separate server blocks and return directives, offers complete configuration examples for both HTTP and HTTPS environments, and discusses performance optimization and SEO considerations.

Introduction

Domain canonicalization is a critical aspect of search engine optimization (SEO) and user experience in web server configuration. Many website administrators aim to standardize domain format, either using the www prefix or the bare domain. This article provides a detailed analysis of correctly implementing www to non-www domain redirection based on the Nginx web server.

Problem Background and Common Errors

Users frequently encounter redirect loop issues when configuring domain redirection in Nginx. The original configuration attempts to handle multiple domains within the same server block:

server {
    listen 80;
    server_name www.example.com example.com;
    root /var/www/www.example.com/web;

    if ($http_host != "www.example.com") {
        rewrite ^ http://example.com$request_uri permanent;
    }
}

This configuration causes redirect loops because when accessing example.com, the condition $http_host not equal to "www.example.com" evaluates to true, triggering redirection to itself.

Correct Configuration Methods

Using Separate Server Blocks

The Nginx official documentation recommends defining separate server blocks for each domain:

server {
    listen 80;
    server_name www.example.com;
    return 301 http://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;
    root /var/www/www.example.com/web;
    # Other configuration items
}

Advantages of Return Directive

Compared to the rewrite directive, the return directive offers better performance:

server {
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

The return directive immediately terminates request processing and returns the redirect, whereas rewrite requires Nginx to perform regular expression matching and processing, adding additional computational overhead.

HTTPS Environment Configuration

In SSL/TLS encrypted environments, configuration must handle both HTTP and HTTPS traffic:

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    
    # Main site configuration
    root /var/www/example.com/web;
    index index.html index.php;
}

DNS Configuration Requirements

Proper DNS record configuration is fundamental to redirection functionality:

example.com.    A    192.192.6.8
www.example.com. A    192.192.6.8

Both domains should point to the same server IP address, ensuring all traffic reaches the Nginx server for processing.

Performance Optimization Considerations

Using the $scheme variable maintains the original request protocol (http or https), avoiding unnecessary protocol conversion:

return 301 $scheme://example.com$request_uri;

This approach ensures users' secure connections (HTTPS) remain secure after redirection, while HTTP connections are correctly redirected.

Configuration Verification and Testing

Before applying configuration changes, always perform syntax checking:

sudo nginx -t

After successful testing, reload the configuration:

sudo systemctl reload nginx

Use curl command to verify redirection works correctly:

curl -I http://www.example.com

Reverse Redirection Configuration

If redirecting from non-www to www domain is required, the configuration approach is similar:

server {
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {
    server_name www.example.com;
    # Main site configuration
}

SEO Best Practices

Using 301 permanent redirects is search engine friendly:

Conclusion

Correct Nginx domain redirection configuration requires defining separate server blocks for each domain, using return directives instead of rewrite, and ensuring DNS records correctly point to the server. This approach not only resolves redirect loop issues but also provides better performance and search engine optimization results. Through the configuration examples and best practices provided in this article, administrators can easily implement canonicalization redirection between www and non-www domains.

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.