Naked Domain Redirect on NGINX

Asif Ahmed

This will be a quick snippet post about a problem I encountered today. What’s the best way to redirect your naked domain (like madpad.me) to “www.madpad.me” in your browser, if you are using nginx as your server?

In nginx you could use an if statement to check and then redirect, but this is bad practice. Recommended practice is just this to add your nginx config file for your specific website:


server {

server_name madpad.me;

return 301 $scheme://www.madpad.me$request_uri;

}

After you add this snippet, then restart your nginx server and all requests to your naked domain should be rerouted through www + your domain.