转到域时的ERR_TOO_MANY_REDIRECTS:Nginx,Daphne,Django,DigitalOcean

时间:2019-03-01 20:21:34

标签: django nginx daphne

我正在使用Daphne和Nginx在DigitalOcean,Ubuntu 16.04上运行Django Channels应用程序。

跟随this post

  

Nginx将仅用作您的Django应用程序的代理,您的   django应用程序将与daphne一起运行。   并且您应该让daphne在127.0.0.1:8001上运行(或更改   移植到您的喜好)。

我已为我的页面启用“让我们加密SSL”,并告诉所有http个请求都重定向到https

我的页面显示错误

myapp.com redirected you too many times.

我正在127.0.0.1:8001上运行daphne。

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

我的nginx配置文件

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;
    }
}

1 个答案:

答案 0 :(得分:0)

未正确设置配置的第一块。 listen 443行应该在第二个块上。尝试这些配置。

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;
    }
}