Setting Up Redirects with Nginx on a VPS

Redirects are vital for managing traffic on your VPS-hosted website, ensuring users reach the right URLs while boosting SEO and security. This guide simplifies configuring Nginx redirects for permanent (301), temporary (302), HTTP-to-HTTPS, www/non-www, and path-specific cases. With practical examples and tips, you’ll master Nginx redirects efficiently.

Prerequisites

  • A VPS with Nginx installed

  • Root or sudo access

  • Your site’s config file (typically in /etc/nginx/sites-available/ or /etc/nginx/conf.d/)

 1. Permanent Redirect (301)

Use this when a URL or domain has changed permanently.

✅ Example: Redirect from old domain to new domain

server {
listen 80;
server_name olddomain.com www.olddomain.com;
return 301 https://newdomain.com$request_uri;
}

🔐 Tip: Always redirect to HTTPS when possible.

2. Temporary Redirect (302)

Use this for temporary redirection (e.g., during maintenance).

server {
listen 80;
server_name olddomain.com www.olddomain.com;
return 301 https://newdomain.com$request_uri;
}

 3. Redirect HTTP to HTTPS

This is highly recommended for SEO and security.

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

Then configure your HTTPS server block separately:

server {
listen 443 ssl;
server_name example.com;
# SSL config here
}

 4. Redirect www to non-www (or reverse)

Choose one preferred version for consistency and SEO.

From www to non-www:

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

Or from non-www to www:

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

 5. Redirect a specific path

Example: Redirect /blog to /articles:

location = /blog {
return 301 /articles;
}

Or with full URL:

location = /blog {
return 301 https://example.com/articles;
}

Where to place these directives

All of the above directives should be placed in the appropriate server block within your site’s configuration file:

/etc/nginx/sites-available/example.com

Then symlink it to sites-enabled if needed:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

After Updating Configurations

Always test your Nginx configuration before reloading:

sudo nginx -t

If there are no errors:

sudo systemctl reload nginx

Bonus: Redirect All 404 Errors to Home Page

error_page 404 =301 /;

Place this inside your server block to handle broken links gracefully.

Nginx is not just a high-performance web server—it’s also a powerful tool for controlling traffic and user experience through intelligent redirects. With just a few lines of configuration, you can enforce HTTPS, guide users to the right URLs, and optimize your site for both SEO and security.

Additional Tips

  • SEO-Friendly: Use 301 redirects to preserve search engine rankings.

  • SSL Setup: Ensure your SSL certificate is valid before HTTPS redirects (use Certbot for Let’s Encrypt).

  • Test Redirects: Use https://www.redirect-checker.org to verify no redirect loops or chains.

  • Logging: Enable access logs (access_log /var/log/nginx/access.log;) to monitor redirect behavior.

Conclusion

Nginx makes redirect management on your VPS simple and powerful, enhancing user experience, security, and SEO. By configuring 301, 302, HTTPS, or path-specific redirects and testing with the provided examples, you can ensure smooth traffic flow. With AvaHost’s reliable VPS hosting and these tips, your site will stay secure, accessible, and optimized for growth.