Nginx - simple permanent or temporary redirects

CaffeineFueled

2022/11/23

Temporary or permanent redirect

First you have to decide whether the redirect will be permanent (301), or just temporary (302). If you are uncertain, just pick temporary and switch later.

Use cases from my understanding:

Permanent 301 redirects:
switching to another domain
merging multiple domains
switching from HTTP to HTTPs
better SEO experience
Temporary 302 redirect:
testing (A/B testing, etc)
single redirects to another domain
redirect to a maintenance page
redirect traffic for load balancing

Both do the same, but still have their use cases. Switching them up could cause problems with various indexes of search engines, SEO, wrongly being flagged as a spammer, and so on.

Simple redirects in nginx

For this example, I am going to use temporary 302 redirects.

Simple redirect of a sub-domain to a single URL

server {
    listen      80;
    listen      443;
    server_name  test2.brrl.net;
    location / {
          return 302 https://www.youtube.com/watch?v=dQw4w9WgXcQ;
    }
}

That is a simple redirect of the root of the sub-domain. Try it out: https://test2.brrl.net.

If you want to create a redirection of a subdirectory like /status, simply change it accordingly:

server {
    listen      80;
    listen      443;
    server_name  brrl.net;
    location /status {
          return 302 https://status.brrl.net/status/overview;
    }
}

With this config block, only the subdirectory /status would be redirected. For example: https://brrl.net/status redirects to https://status.brrl.net/status/overview.

other redirects

There are many more forms of redirects, but I am familiar enough to write about that. I might add more redirects later on, but I’ll have to test beforehand.




Most recent Articles:
  • Notice Board 003: Progress is Progress
  • How to: Cisco ISE backup to SFTP repository with public key authentication
  • Dummy IP & MAC Addresses for Documentation & Sanitization
  • Deploying ISSO Commenting System for Static Content using Docker
  • Generate a Vanity v3 Hidden Service Onion Address with mkp224o