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.

Jay is founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or drawing on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

You can leave a comment on my original post.