How to Redirect to a Different Domain Using Nginx?
Redirecting to a different domain using Nginx is a common task for handling changes in domain names, ensuring consistency, or managing URL structures. Nginx makes this process straightforward with its return and rewrite directives. Here’s how you can set up a redirect to a different domain using Nginx:
1. Simple Domain Redirection
If you want to redirect all traffic from one domain to another, you can use the return directive. This is the simplest method for performing a 301 (permanent) or 302 (temporary) redirect.
Example:
Redirect all traffic from example1.com to example2.com with a permanent redirect:
server {
listen 80;
server_name example1.com;
location / {
return 301 $scheme://example2.com$request_uri;
}
}
Explanation:
listen 80;: Specifies that Nginx should listen on port 80 (HTTP).server_name example1.com;: Defines the domain that should be redirected.return 301 $scheme://example2.com$request_uri;: Redirects with a 301 status code.$schemeensures the correct protocol (httporhttps) is used, and$request_uripreserves the original request path and query string.
2. Redirecting HTTP to HTTPS
To redirect all HTTP traffic to HTTPS for a domain, you can use a server block to handle HTTP requests and redirect them to HTTPS.
Example:
server {
listen 80;
server_name example.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
location / {
# Your SSL configuration here
}
}
Explanation:
listen 80;: Handles HTTP requests.return 301 https://$host$request_uri;: Redirects HTTP to HTTPS, preserving the host and URI.listen 443 ssl;: Handles HTTPS requests.
3. Redirect with rewrite Directive
For more complex redirection scenarios, you can use the rewrite directive. This is useful for changing URL paths or patterns before performing the redirect.
Example:
Redirect requests from example.com/oldpath to example2.com/newpath:
server {
listen 80;
server_name example.com;
location /oldpath {
rewrite ^/oldpath/(.*)$ <https://example2.com/newpath/$1> permanent;
}
}
Explanation:
rewrite ^/oldpath/(.*)$ <https://example2.com/newpath/$1> permanent;: Therewritedirective matches requests starting with/oldpathand redirects them to/newpath, preserving the rest of the URL path. Thepermanentkeyword specifies a 301 redirect.
4. Redirect Based on Conditions
You can also use conditional redirects based on specific request characteristics.
Example:
Redirect requests based on the User-Agent header:
server {
listen 80;
server_name example.com;
if ($http_user_agent ~* "mobile") {
return 302 https://m.example.com$request_uri;
}
location / {
return 301 https://www.example.com$request_uri;
}
}
Explanation:
if ($http_user_agent ~* "mobile"): Checks if theUser-Agentheader contains the word "mobile".return 302 https://m.example.com$request_uri;: Redirects mobile users to a mobile-specific subdomain.
5. Redirect with a Custom Status Code
You can use different status codes based on the nature of the redirect.
Example:
Redirect from oldpage.html to newpage.html with a 302 (temporary) redirect:
server {
listen 80;
server_name example.com;
location /oldpage.html {
return 302 /newpage.html;
}
}
Explanation:
return 302 /newpage.html;: Performs a temporary redirect fromoldpage.htmltonewpage.html.
6. Applying Redirects with SSL
If your site uses SSL, make sure the server block for HTTPS is properly configured to handle the redirects.
Example:
Redirect HTTP to HTTPS with SSL:
server {
listen 80;
server_name example.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
location / {
# Your SSL configuration here
}
}
7. Reload Nginx Configuration
After updating your Nginx configuration, reload Nginx to apply the changes:
sudo nginx -t # Test the configuration for syntax errors
sudo systemctl reload nginx # Reload Nginx with the new configuration
Summary
To redirect to a different domain using Nginx:
- Use
returnfor Simple Redirects: Usereturn 301or302to redirect traffic from one domain to another. - Redirect HTTP to HTTPS: Use a separate server block to redirect HTTP requests to HTTPS.
- Apply
rewritefor Path Changes: Userewritefor more complex URL patterns and redirections. - Handle Conditional Redirects: Use
ifdirectives for conditional redirects based on request attributes. - Apply SSL Configurations: Ensure HTTPS configurations are correctly set up to handle secure redirects.
By following these steps, you can configure Nginx to effectively manage redirects to different domains and handle various redirection scenarios.