Keywords: Nginx_redirection | domain_configuration | server_name_directive
Abstract: This article provides an in-depth exploration of domain redirection techniques in Nginx server configuration, focusing on suffix matching with server_name directive and the differences between rewrite and return methods. Through detailed configuration examples and technical analysis, readers will understand the core principles of Nginx redirection mechanisms and master best practices for handling main domain and all subdomain redirects.
Overview of Nginx Redirection Mechanism
Domain redirection is a fundamental yet crucial functionality in web server configuration. As a high-performance HTTP server, Nginx offers flexible redirection configuration options. The core purpose of redirection is to automatically guide user requests from one URL to another, which is particularly important in scenarios such as website migration, domain name changes, or URL normalization.
Suffix Matching with server_name Directive
Nginx's server_name directive supports multiple matching patterns, with suffix matching being an effective approach for batch domain processing. Using the .mydomain.example syntax matches mydomain.example and all its subdomains, including *.mydomain.example. This matching mechanism is based on string comparison, where the corresponding server block is selected to handle the request when the Host header information matches in suffix form.
Comparison of Redirection Implementation Methods
In Nginx configuration, there are two primary methods to implement HTTP redirection:
Using rewrite Directive
The rewrite directive was commonly used in earlier versions of Nginx for redirection. Its basic syntax is:
server {
server_name .mydomain.example;
rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}
Here, ^ matches all request paths, the $request_uri variable preserves the original request URI, and the permanent parameter specifies the 301 permanent redirect status code. While this method is powerful, it is less performant compared to dedicated return directives.
Using return Directive (Recommended)
For Nginx version 0.9.1 and above, using the return directive for redirection is recommended:
server {
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
return 301 directly returns the 301 status code and the redirect target URL. This approach is more efficient as it avoids the overhead of regular expression matching. The $request_uri variable ensures that the original request's query parameters and path are retained.
In-Depth Analysis of Configuration Example
Let's analyze the recommended configuration in detail:
server {
listen 80;
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
In this configuration:
listen 80specifies the server to listen on HTTP portserver_name .mydomain.examplematches all domains ending withmydomain.examplereturn 301returns the 301 permanent redirect status code- The target URL is constructed using the full protocol, domain, and original request URI
Technical Points and Best Practices
Several key considerations are important in practical deployment:
Status Code Selection
301 redirect indicates a permanent move, prompting browsers and search engines to update their records. If the redirection is temporary, consider using the 302 status code instead.
Performance Considerations
The return directive offers better performance compared to rewrite, especially in high-concurrency scenarios. It directly returns the redirect response without going through complex regex matching processes.
Protocol Handling
If the target website uses HTTPS, the redirect URL should be changed to https://www.adifferentdomain.example$request_uri to maintain the integrity of secure connections.
Error Handling and Debugging
Common errors when configuring redirections include:
- Forgetting to use the dot prefix in server_name for suffix matching
- Incorrect escaping of special characters leading to configuration parsing errors
- Omitting the
$request_urivariable resulting in lost path information
Use the nginx -t command to test configuration file syntax correctness and verify redirection behavior through browser developer tools.
Extended Application Scenarios
Beyond basic domain redirection, this configuration pattern can be applied to:
- Unified redirection of multiple old domains to a new domain
- Redirecting non-www domains to www versions (or vice versa)
- Handling redirections for specific paths
- Geographically differentiated redirections
By mastering Nginx redirection configuration techniques, website administrators can flexibly address various domain migration and URL optimization needs, ensuring continuity in user experience and search engine optimization.