Nginx err too many redirects error after ssl configuration

gunicornnginx

I have googled for the past 6 hours and not found what is wrong with my nginx.conf. My setup is django>gunicorn>nginx. my nginx.conf is below (Due to many trials is a bit messy):

upstream app_server {
    server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
    listen 80 ;
    listen [::]:80  ipv6only=on;
    server_name comparebet.co.ke www.comparebet.co.ke;

    return 301 https://$server_name$request_uri;

    client_max_body_size 4G;
    keepalive_timeout 5;

    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    access_log /var/log/access.log;
    error_log /var/log/error.log;
}

server  {
    listen 443;
    ssl on;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name comparebet.co.ke;

    root /usr/share/nginx/html;
    index index.html index.htm;

    location /static {
        alias /home/django/django_project/django_project/static;
    }

    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_buffering off;
        proxy_pass http://comparebet.co.ke;
    }

    location ~ /.well-known {
        allow all;
    }

    access_log /var/log/access.log;
    error_log /var/log/error.log;
}

I will appreciate any help. Thanks

Edited to include curl response headers

* Rebuilt URL to: comparebet.co.ke/
*   Trying 178.62.11.22...
* Connected to comparebet.co.ke (178.62.11.22) port 80 (#0)
> GET / HTTP/1.1
> Host: comparebet.co.ke
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.10.0 (Ubuntu)
< Date: Wed, 22 Mar 2017 00:18:58 GMT
< Content-Type: text/html
< Content-Length: 194
< Connection: keep-alive
< Location: https://comparebet.co.ke/
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>
* Connection #0 to host comparebet.co.ke left intact

Main issue is when domain is accessed by Chrome browser it returns "ERR: TOO MANY REDIRECTS". This is what I want to solve

Best Answer

Your https server contains these line (excerpt). This tells it to send any requests going to the https site back to the http site

location / {
    proxy_pass http://comparebet.co.ke;
}

Your http server contains these lines, forwarding requests back to the https site.

server_name comparebet.co.ke www.comparebet.co.ke;
return 301 https://$server_name$request_uri;

Nginx is doing exactly what you told it to do, which is to create a redirect loop. The solution depends on what you're trying to do.