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
🔐 Tip: Always redirect to HTTPS when possible.
2. Temporary Redirect (302)
Use this for temporary redirection (e.g., during maintenance).