Skip to content

Latest commit

 

History

History
109 lines (73 loc) · 1.8 KB

redirects.md

File metadata and controls

109 lines (73 loc) · 1.8 KB

nginx.conf example redirects

www to non-www and http to https redirects in one server block

This will redirect:

server {
  server_name example.com www.example.com;
  listen 80;
  listen 443 ssl;

...

  if ($scheme = http) {
     return 301 https://$server_name$request_uri;
  }

  if ($host ~ ^www\.) {
     return 301 https://$server_name$request_uri;
  }

...

}

However, it can easily go wrong if you swap server names in server_name directive:

# WRONG CONFIGURATION! DO NOT COPY/PASTE IT ON YOUR SERVER!
server {
  server_name www.example.com example.com;
  listen 80;
  listen 443 ssl;

...

  if ($scheme = http) {
     return 301 https://$server_name$request_uri;
  }

  if ($host ~ ^www\.) {
     return 301 https://$server_name$request_uri; # WRONG! Infinite redirect.
  }

...

}

Here is more robust solution:

server {
  server_name www.example.com example.com;
  listen 80;
  listen 443 ssl;

...

  if ($scheme = http) {
     return 301 https://$server_name$request_uri;
  }

  if ($host ~ ^www\.) {
     return 301 https://example.com$request_uri; # Do not forget to replace example.com with your own domain name.
  }

...

}

This feature can be achieved using different server blocks like below, but may break some control panels that does not tolerate big manual changes to config files.

Redirects using many server blocks

server {
  listen 443 ssl;
  server_name example.com;
 
  ...

}

server {
  listen 443 ssl;
  server_name www.example.com;
  return 301 https://example.com$request_uri;

  ...

}

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