0

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

  • Please edit your question to more clearly state your problem, and show matching curl (with response headers) and logs demonstrating the problem. Please also tidy up your configuration so it's easier to read. – Tim Mar 22 '17 at 0:10
  • Edited the nginx.conf file and included the curl response headers – Lawrence Muriuki Mar 22 '17 at 0:21
  • Right, so now we need to see the matching log entry from Nginx, django, and gunicorn, to work how which piece of software is sending the 301. – Tim Mar 22 '17 at 0:28
1

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.

| improve this answer | |
  • I know that solution for @LawrenceMuriuki is found, but I'd like to help with a similiar issue I recenly faced and this answer gave me a clue. So I changed proxy_set_header X-Forwarded-Proto http; to proxy_set_header X-Forwarded-Proto https; – Alexander Gorg Sep 21 '18 at 19:46

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.