Setting Up Redirects with Nginx on a VPS
Popular:
ā K
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.
Table of Contents
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.


