1

I am trying to do the following things

  • force www. in my urls
  • 301 redirect to https

I am getting too many redirects error when i go to www.example.com. When i go to example.com, it works fine.

Not sure where i went wrong. below is my server block code.

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


server {
    server_name www.mydomain.com
    ...
}
  • 1
    Fix up your question please. It has example.com, www.example.com, and mydomain.com which seems unrelated. – Tim Jun 5 '16 at 3:50
5

Here's how I do redirects to https://www.example.com

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

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  // locations etc
}

# This server simply redirects the requested to the https version of the page
server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

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

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  return 301 https://www.example.com$request_uri;
}
  • thanks for sharing the configuration! appreciate it greatly. – Slay Jun 6 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.