Nginx redirect quickstart

Using the return directive, clients can easily be returned a HTTP code and an optional URL. For example, if you’re redirect all HTTP traffic to HTTPS or www to non-www, returning 301 Moved Permanently is appropriate.

Some guides recommend including an if statement in your existing server block. However, redirection is more efficient if two separate server blocks are used.

Here the first server block is listening on the www subdomain and simply passes any request on to the root domain that has been configured to actually serve your site. That’s all it takes for a basic Nginx redirect.