How to Use Nginx Redirect to Another URL? (Nginx URL Redirect)
HTTP redirection or Nginx redirects is a technique used to send users and browsers from one web address to another. It's often used when a website changes its structure or domain. There are several types of redirects, and each one tells the browser something different about the change.
The two most commonly used types are: Temporary redirects (HTTP status code 302): These are ideal for situations where content is temporarily moved. For example, during maintenance, you might redirect users to a temporary page that explains the downtime.
Nginx Permanent redirects (HTTP status code Nginx 301):
These let browsers and search engines know that a page has been permanently moved. This is typically used when switching to a new domain or restructuring a site permanently. This guide will dive into how to configure both types of redirects using NGINX and provide practical examples for common redirection scenarios.
Prerequisites
Before starting this tutorial, make sure you have the following:
- A server with NGINX installed and properly configured to host your website(s).
- If you need help setting this up, you can refer to the setup guides available for operating systems like Ubuntu 22.04, Debian, or CentOS.
What is Nginx Redirect?
In NGINX, a redirect is a server-side feature used to send incoming requests from one URL to another. Redirects play a crucial role when updating site structure, changing domains, or enforcing consistent URL formats. NGINX supports different types of redirects, most commonly used as permanent (301) and temporary (302).
Redirects can be configured in the NGINX server block (also known as the virtual host or vhost file). Common use cases include:
- Redirecting HTTP traffic to HTTPS for secure connections
- Forcing URLs to use either the www or non-www version
- Setting up rules for temporary or permanent redirection based on URL patterns
This lets you control traffic flow to your site and helps improve security, SEO, and user experience.
How does Nginx redirect to another URL?
NGINX makes it simple to create redirects using the built-in rewrite directive, which is available by default. You can use it to define both temporary and permanent redirects by specifying a pattern to match and the new destination URL.
Example 1: Temporary Redirect of a Specific Page
The following server block redirects requests for /old-page on example-site.com to /maintenance.html on the same domain:
server {
listen 80;
server_name example-site.com;
location /old-page {
rewrite ^/old-page$ /maintenance.html redirect;
}
}
This sets up a temporary (302) redirect, which is useful when a page is unavailable for a short period, such as during maintenance.
Example 2: Temporary Redirect of All Traffic to Another Domain
To forward all traffic from blog.example.com to archive.example.net while preserving the request path:
server {
listen 80;
server_name blog.example.com;
rewrite ^/(.*)$ https://archive.example.net/$1 redirect;
}
This will send every request to the new domain with the same path, but as a temporary redirect.
Example 3: Permanent Redirect from HTTP to HTTPS
Force all HTTP traffic to use HTTPS with a 301 redirect:
server {
listen 80;
server_name mysite.org;
return 301 https://$host$request_uri;
}
This is a best practice for improving security and SEO.
Example 4: Permanent Redirect from Non-WWW to WWW
If you want to redirect all traffic from example.org to www.example.org permanently:
server {
listen 80;
server_name example.org;
return 301 http://www.example.org$request_uri;
}
This ensures that users and search engines always access the preferred domain.
Redirecting to a New Domain
When transitioning your website to a new domain, it's important not to leave the old one inactive. Without redirection, any bookmarks, external links, or indexed pages will lead to broken URLs, potentially costing you traffic and harming your SEO.
In this section, we’ll walk through how to permanently redirect traffic from your old domain (e.g., domain1.com) to your new one (domain2.com) using NGINX. Since the move is permanent, we’ll use a 301 redirect to ensure that search engines and browsers treat the new domain as the official destination going forward.
Let’s assume you already have two server blocks configured in NGINX:
Old Domain: /etc/nginx/sites-available/domain1.com
server {
listen 80;
server_name domain1.com;
# Existing configuration
}
New Domain: /etc/nginx/sites-available/domain2.com
server {
listen 80;
server_name domain2.com;
# New site setup
}
Applying the Redirect
Now, you need to modify the configuration for the old domain to send all traffic to the new domain using a permanent redirect:
server {
listen 80;
server_name domain1.com;
rewrite ^/(.*)$ https://domain2.com/$1 permanent;
}
In this configuration:
- The regular expression ^/(.*)$ captures everything after the root (/) in the incoming URL.
- The $1 in the destination URL inserts that captured path into the redirect.
- Adding permanent at the end of the rewrite statement tells NGINX to send a 301 HTTP status code, which browsers and search engines interpret as a permanent move.
For example, a request to http://domain1.com/contact.html will automatically redirect to https://domain2.com/contact.html.
Reload Nginx Configuration
Once you've made this change, remember to validate and reload your NGINX configuration:
$ sudo nginx -t
$ sudo systemctl reload nginx
This ensures your new redirection rule is active and that all visitors—and search engines—are correctly routed to the new domain.
Conclusion
You now understand how to set up redirects in NGINX. It's important to choose the right type—permanent or temporary, as the wrong one can impact SEO.
Nginx Redirects are also useful for enforcing HTTPS and standardising URLs (like adding www.). When used properly, they help maintain your site's traffic while giving you flexibility to change its structure.
For deeper insights, refer to the official NGINX documentation, especially the sections on the rewrite module and redirect guides.
BlueVPS offers powerful VPS servers with full RDP root access, giving you complete administrative control when you choose a Windows-based server. Our VPS solutions come equipped with high-performance, enterprise-grade SSDs from top brands like Samsung, Kingston, and Intel, ensuring fast and reliable storage.
BlueVPS makes scaling easy — you can quickly upgrade your server resources whenever your needs grow, with minimal downtime. Experience flexible and robust VPS hosting tailored to your demands.
Discover reliable and scalable VPS hosting with full root access at BlueVPS.
Blog