How to redirect a URL in NGINX

I’m a fan of easy to remember URLs, rather than long cryptic ones. For things I give our verbally or in chat I sometimes setup redirects using one of my domains and NGINX directives.

So when I’d like to point users to my 3D Shenanigans channel, I can tell them to go to https://3ds.wpguru.tv rather than https://www.youtube.com/channel/UCmBV6fLZMzCaIEXzF1SSJuQ/ (because which human in their right mind could possibly remember the latter).

Thing is, I can never remember what the exact syntax is, so I thought I’d write myself a note for such future redirection setup. Here’s what the above example looks like when added as a directive to the subdomain:

location = / {<br>
    return https://www.youtube.com/channel/UCmBV6fLZMzCaIEXzF1SSJuQ/;<br>
}

Note the use of the semicolon at the end of the line and the curly braces. I’m not that versed in NGINX, but every time I look at it I think “that’s easy”, and promptly forget only seconds later. I hope this helps both you and me to remember next time.

You can leave a comment on my original post.