Answered by
Oliver Hall
A 302 redirect in Nginx is used to temporarily redirect traffic from one URL to another. This type of redirect informs browsers and search engines that the redirect may change again in the future, hence it's temporary.
To implement a 302 redirect in Nginx, you need to modify the Nginx configuration file (typically found at /etc/nginx/nginx.conf
or within the /etc/nginx/sites-available/
directory). The basic syntax for a 302 redirect looks like this:
server { listen 80; server_name www.example.com; location /oldpage { return 302 http://www.example.com/newpage; } }
In this example:
server_name
specifies the domain.location
block, /oldpage
is the URI that you want to redirect from.return 302 http://www.example.com/newpage;
indicates the 302 redirect; requests to /oldpage
will be redirected to /newpage
.server
block if not present, or add a location
block inside an existing server
block as shown above.sudo nginx -t
to ensure there are no syntax errors in your configuration files.sudo systemctl reload nginx
or sudo service nginx reload
.By properly implementing a 302 redirect, you can manage the temporary movement of web traffic without adversely impacting your site's SEO.