0

Sorry of the messy conf file. I have been trying everything, so it's become a mess. I want to enable SSL and redirect all traffic to https. Currently, with the below conf file, I can navigate to HTTP with no issues. HTTPS just redirects back to HTTP. If I uncomment line 5, then the browser simply says too many redirects. Any help is appreciated.

UPDATE: Based on the comment below, I updated the conf file. Same issue. The minute I add return 301 I get too many redirects error.

    # Redirect all variations to https://www domain
server {
  listen 80;
  server_name name.com www.name.com;
 # return 301 https://www.name.com$request_uri;
}

server {
  listen 443 ssl;
  server_name name.com;

  ssl_certificate /etc/letsencrypt/live/name.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/name.com/privkey.pem;

  # 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;

  # This is a cache for SSL connections
  ssl_session_cache shared:SSL:2m;
  ssl_session_timeout 60m;


 #return 301 https://www.name.com$request_uri;
}

marked as duplicate by dawud, womble Jun 6 '16 at 0:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

You need two server blocks for redirection (as per below), then your main block that services actual requests (not included below as it depends on your application / usage).

# Redirect all variations to https://www domain
server {
  listen 80;
  access_log  /var/log/nginx/example.access.log main buffer=128k flush=60 if=$log_ua;
  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/xx/fullchain;
  ssl_certificate_key /var/lib/acme/certs/xx/privkey;

  access_log  /var/log/nginx/example.access.log main buffer=128k flush=60 if=$log_ua;

  # 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;

  # This is a cache for SSL connections
  ssl_session_cache shared:SSL:2m;
  ssl_session_timeout 60m;


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

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