Deepak Dargade

I'm a Mumbai-based Entrepreneur, Ruby on Rails monomaniac and Food enthusiast.

07 Dec 2013

Nginx 301 redirect (non-www to www – traditional way)

server{
  server_name domain.com;
  rewrite ^(.*) https://www.domain.com$1 permanent;
}

You should never use a rewrite.

Why? Because nginx has to process and start a search.

But if you use return, it directly stops execution. This is more preferred.

Nginx 301 redirect (non-www to www – new, efficient, & the best way)

server{
  listen  80;
  server_name  domain.com;
  return  301 $scheme://www.domain.com$request_uri;
}

server{
  listen 443;
  server_name  domain.com;
  ssl on;
  ssl_certificate path_to_ssl.crt;
  ssl_certificate_key path_to_ssl.key;
  return 301 https://www.domain.com$request_uri;
}

server{
  listen 443;
  server_name www.domain.com;
  root /var/www/student-performance/public;
  ssl on;
  ssl_certificate path_to_ssl.crt;
  ssl_certificate_key path_to_ssl.key;
  passenger_enabled on;

  #rest of configuration

}


Looking for comments?

I don't have comments on this site as they're difficult to manage and take up too much time. I'd rather concentrate on producing content than managing comments.

Since there are no comments, feel free to contact me ✉️ contact me instead.