Petru
Petru

Reputation: 520

Nginx too many redirects when redirecting http to https

I'm trying to redirect http to https. I use letsencrypt for ssl certificates. My config looks like this

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

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 5m;

        server_name example.com www.example.com;

        root /var/www/landing;

        location /.well-known/ {
                root /var/www/;
        }
}

When I'm trying to access example.com, I get a browser error saying that there were too many redirects. The error occurs for both http://example.com and https://example.com, the server block is accessed when I go to http://www.example.com because I get redirected to https://example.com and then I get the error above.

How can I fix this?

Upvotes: 5

Views: 4875

Answers (1)

Petru
Petru

Reputation: 520

After I did wget -S https://wellcode.com I assumed that the problem was on the dns so in Cloudflare I changed SSL to full and the problem was solved.

Explanation:

The -S flag will output headers and therefore show you the redirects. Example:

  HTTP/1.1 301 Moved Permanently
  Server: nginx
  Date: Tue, 05 Jan 2021 12:26:55 GMT
  Content-Type: text/html
  Content-Length: 162
  Connection: close
  Location: https://example.com/foo?bar=baz&dragons=probably
  HTTP/1.1 200 OK
  Server: nginx
  Date: Tue, 05 Jan 2021 12:26:55 GMT
  Content-Type: application/json; charset=utf-8
  Transfer-Encoding: chunked
  Connection: close
  Vary: Accept-Encoding
  X-Powered-By: PHP/7.4.13
  Expires: Tue, 05 Jan 2021 12:26:55 GMT
  Cache-Control: max-age=0
  X-Content-Type-Options: nosniff
  Strict-Transport-Security: max-age=16070400; includeSubDomains

Upvotes: 4

Related Questions