title: ERR_TOO_MANY_REDIRECTS on Nginx
body: <p>I just updated my Let's Encrypt subscription with the command </p>
  
  <pre><code>sudo /usr/local/bin/certbot-auto --nginx
  </code></pre>
  
  <p>this gave an error which was solved by removing the line </p>
  
  <pre><code># proxy_redirect http:// https://;
  </code></pre>
  
  <p>from /etc/nginx/sites-available/default, which I thought is fine, since it looks to me like a comment anyway. However, now my website does not load anymore and gives the error </p>
  
  <pre><code>This page isn’t working 
  example.com redirected you too many times.
  Try clearing your cookies.
  ERR_TOO_MANY_REDIRECTS
  </code></pre>
  
  <p>I tried to add the line back in, but that did not help. Any thoughts in what could have gone wrong?</p>
  
  <p>here is my /etc/nginx/sites-available/default</p>
  
  <pre><code>server {
      listen 80 default_server;
  
      server_name example.com www.example.com;
  
      return 301 https://$server_name$request_uri;
    }
  
  server {
  
      listen 443 ssl; # managed by Certbot
      ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  
      ssl_dhparam /etc/ssl/certs/dhparam.pem;
  
      # Settings to serve static files
      location /static/  {
  
          # Example:
          # root /full/path/to/application/static/file/dir;
          root /var/www/example/app/;
  
          location ~*  \.(jpg|woff|jpeg|png|gif|ico|css)$ {
              expires 30d;
          }
  
          location ~*  \.(js)$ {
              expires 1d;
          }
  
          # we do not cache html, xml or json
          location ~* \.(?:manifest|appcache|html?|xml|json)$ {
              expires -1;
              # access_log logs/static.log; # I don't usually include a static log
          }
  
          location ~*  \.(pdf)$ {
              expires 30d;
          }
      }
  
      # Serve a static file (ex. favico)
      # outside /static directory
      location = /favico.ico  {
  
          root /app/favico.ico;
          gzip_static on;
      }
  
      # Proxy connections to the application servers
      # app_servers
      location / {
          proxy_connect_timeout 300s;
          proxy_read_timeout 300s;
          proxy_pass         http://app_servers;
          proxy_redirect     off;
          # proxy_redirect http:// https://;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
      }
  }
  </code></pre>
  
  <p>here is my /etc/nginx/nginx.conf</p>
  
  <pre><code>worker_processes 1;
  
  events {
     worker_connections 1024;
  }
  
  http {
     include /etc/nginx/mime.types;
  
     sendfile on;
  
     gzip              on;
     gzip_http_version 1.1;
     gzip_comp_level   5;
     gzip_proxied      any;
     gzip_min_length   256;
     gzip_vary         on;
       gzip_types
       application/atom+xml
       application/javascript
       application/json
       application/rss+xml
       application/vnd.ms-fontobject
       application/x-font-ttf
       application/x-web-app-manifest+json
       application/xhtml+xml
       application/xml
       font/opentype
       image/svg+xml
       image/x-icon
       text/css
       text/plain
       text/x-component;
  
     # Configuration containing list of application servers
     upstream app_servers {
  
         server 127.0.0.1:8080;
         # server 127.0.0.1:8081;
         # ..
         # .
  
     }
  
     # Configuration for Nginx
     server {
  
         # Running port
  
     # This is required by let's encrypt to recognize the domain (see also let's encrypt lines below)
     server_name example.com www.example.com;
  
         return 301 https://$server_name$request_uri;
  
     listen 443 ssl; # managed by Certbot
     ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
  
     server {
  
     listen 443 ssl; # managed by Certbot
         ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
         ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
         include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  
     # add Strict-Transport-Security to prevent man in the middle attacks
         add_header Strict-Transport-Security "max-age=31536000";
  
         ssl_dhparam /etc/ssl/certs/dhparam.pem;
  
         # Settings to serve static files 
         location /static/  {
  
             # Example:
             # root /full/path/to/application/static/file/dir;
             root /var/www/example/app/;
  
             location ~*  \.(jpg|woff|jpeg|png|gif|ico|css)$ {
                 expires 30d;
             }
  
         location ~*  \.(js)$ {
         expires 1d;
         }
  
             # we do not cache html, xml or json
      location ~* \.(?:manifest|appcache|html?|xml|json)$ {
                 expires -1;
                 # access_log logs/static.log; # I don't usually include a static log
             }
  
             location ~*  \.(pdf)$ {
                 expires 30d;
             }
     }
  
         # Serve a static file (ex. favico)
         # outside /static directory
         location = /favico.ico  {
  
             root /app/favico.ico;
         gzip_static on;
         }
  
     # Proxy connections to the application servers
         # app_servers
         location / {
         proxy_connect_timeout 300s;
             proxy_read_timeout 300s;
         proxy_pass         http://app_servers;
             proxy_redirect     off;
             # proxy_redirect http:// https://;
         proxy_set_header   Host $host;
             proxy_set_header   X-Real-IP $remote_addr;
             proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header   X-Forwarded-Host $server_name;
         }
  }
  
  server {
  if ($host = www.example.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot
  
  
  if ($host = example.com) {
      return 301 https://$host$request_uri;
  } # managed by Certbot
  
  
      listen 80;
  server_name example.com www.example.com;
  return 404; # managed by Certbot
  }}
  </code></pre>
  
  <p>I also used <a href="http://www.redirect-checker.org/" rel="nofollow noreferrer">http://www.redirect-checker.org/</a> which gave</p>
  
  <pre><code>Result
  https://www.benty-fields.com
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  https://benty-fields.com/
  301 Moved Permanently
  
  Problems found:
  Too many redirects. Please try to reduce your number of redirects for https://www.benty-fields.com. Actually you use 19 Redirects. Ideally you should not use more than 3 Redirects in a redirect chain. More than 3 redirections will produce unnecessary load on your server and reduces speed, which ends up in bad user experience.
  </code></pre>
  
  <p>since /etc/nginx/sites-available/default includes /etc/letsencrypt/options-ssl-nginx.conf here is the content of that file </p>
  
  <pre><code># This file contains important security parameters. If you modify this file
  # manually, Certbot will be unable to automatically provide future security
  # updates. Instead, Certbot will print and log an error message with a path to
  # the up-to-date file that you will need to refer to when manually updating
  # this file.
  
  ssl_session_cache shared:le_nginx_SSL:10m;
  ssl_session_timeout 1440m;
  
  ssl_protocols TLSv1.2;
  ssl_prefer_server_ciphers off;
  
  ssl_ciphers "**";
  </code></pre>