0

I'm running a Django Channels app on DigitalOcean, Ubuntu 16.04 using Daphne and Nginx.

Followed this post.

Nginx will only be used as a proxy for your django application, your django application will be running with daphne. And you should have daphne running on 127.0.0.1:8001 (or change the port to your likings).

I have enabled Let’s Encrypt SSL for my page and told all http requests to be redirected to https.

My page is showing the error

myapp.com redirected you too many times.

I'm running daphne on 127.0.0.1:8001.

daphne -b 127.0.0.1 -p 8001 myapp.asgi:application

My nginx config file

server {
    server_name myapp.com www.myapp.com;
    server_tokens off;
    return 301 https://$server_name$request_uri;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.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 {
    if ($host = www.myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name myapp.com www.myapp.com;
    return 404; # managed by Certbot

    root /home/me/myapp/src/myapp;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/me/myapp/src/myapp;
    }

    location /media/  {
        root /home/me/myapp/src/myapp;
    }

    location / {
        try_files $uri $uri/ @python_django;
    }

    location @python_django {
        proxy_pass http://127.0.0.1:8001;
        proxy_pass_request_headers on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
    }
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
Trilla
  • 943
  • 2
  • 15
  • 31
  • Your configuration file has the main location blocks in the wrong server block. The configuration from your previous question looked correct except for line 2 which needed to be deleted. – Richard Smith Mar 01 '19 at 21:43

1 Answers1

0

The first block of your configuration is not properly set. The listen 443 line is supposed to be on the second block. Try to these configurations.

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

server {
    listen 443 ssl; # managed by Certbot
    server_name myapp.com www.myapp.com;

    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.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  

    root /home/me/myapp/src/myapp;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/me/myapp/src/myapp;
    }

    location /media/  {
        root /home/me/myapp/src/myapp;
    }

    location / {
        try_files $uri $uri/ @python_django;
    }

    location @python_django {
        proxy_pass http://127.0.0.1:8001;
        proxy_pass_request_headers on;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
    }
}
H Dox
  • 655
  • 5
  • 9
  • Thanks very much for your help. I changed my code and then got `directory index of "/home/me/myapp/src/myapp/" is forbidden` then changed `try_files $uri $uri/ @python_django;` to `try_files $uri / @python_django;` as another post suggested and am back to `err- too many redirects` and nothing in the nginx error log. – Trilla Mar 02 '19 at 19:09