ERR_TOO_MANY_REDIRECTS 当我去域:Nginx,Daphne,Django,DigitalOcean

Posted

技术标签:

【中文标题】ERR_TOO_MANY_REDIRECTS 当我去域:Nginx,Daphne,Django,DigitalOcean【英文标题】:ERR_TOO_MANY_REDIRECTS when I go to domain: Nginx, Daphne, Django, DigitalOcean 【发布时间】:2019-07-23 21:45:23 【问题描述】:

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

已关注this post。

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

我已为我的页面启用 Let's Encrypt SSL,并告诉所有 http 请求重定向到 https

我的页面显示错误

myapp.com redirected you too many times.

我正在127.0.0.1:8001 上运行达芙妮。

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;
    

【问题讨论】:

您的配置文件的主要位置块位于错误的服务器块中。您上一个问题的配置看起来是正确的,除了需要删除的第 2 行。 【参考方案1】:

您的配置的第一个块未正确设置。 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;
    

【讨论】:

非常感谢您的帮助。我更改了我的代码,然后得到了directory index of "/home/me/myapp/src/myapp/" is forbidden,然后将try_files $uri $uri/ @python_django; 更改为try_files $uri / @python_django;,正如另一篇文章所建议的那样,我又回到了err- too many redirects,并且 nginx 错误日志中没有任何内容。

以上是关于ERR_TOO_MANY_REDIRECTS 当我去域:Nginx,Daphne,Django,DigitalOcean的主要内容,如果未能解决你的问题,请参考以下文章

ERR_TOO_MANY_REDIRECTS laravel 管理/登录页面

Spring Boot OAuth2,使用 Tomcat 和 nginx 验证后出现错误 ERR_TOO_MANY_REDIRECTS

Chrome 上的 NotFound net::ERR_TOO_MANY_REDIRECTS,SEC7127:针对 IE 上的 CORS 请求阻止重定向

错误 310 (net::ERR_TOO_MANY_REDIRECTS):

Spring Boot Angular 登录错误“ERR_TOO_MANY_REDIRECTS”

使用 Nginx 的 ERR_TOO_MANY_REDIRECTS