Redirecing all Nginx traffic to a canonical subdomain

1.12.2014 by Vladislav Supalov

On this site, I redirect all undirected trafic to the blog subdomain. This happens with the following Nginx configuration snippet:

server {
    listen      80;
    server_name th4t.net;
    #rewrite ^/(.*) http://blog.th4t.net/$1 redirect;
    return  301 http://blog.th4t.net$request_uri;

Any request aimed at the th4t.net domain which is not caught by a more specific rule, is redirected to a corresponding blog.th4t.net url. The commented-out rewrite is what one would want to use coming from an Apache background. When working with Nginx, rewrites are discouraged. Thus, the redirect via return code is the way to go. The 301 response code can be decoded as Moved Permanently. It was motivated by the goal to get rid of ambiguous links to the same content and redirect them to a single subdomain. If the goal were to catch undirected traffic, a 307 code might be more appropriate.

Join the mailing list!

Subscribe to get notified about future articles and stay in touch via email.

I write about Kubernetes, Docker, automation- and deployment topics, but would also like to keep you up to date about news around the business-side of things.

Privacy and your data: You can get more information about the usage of your data, the storage of your registration, sending out mails with the US-provider ConvertKit, statistical analysis of emails sent and your possibility to unsubscribe in my Privacy Policy.

I use the US-provider ConvertKit for email automation. By clicking to submit this form, you acknowledge that the information you provide will be transferred to ConvertKit for processing in accordance with their Privacy Policy and Terms.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit