ssl 配置后,Nginx err too many redirects 错误

ssl 配置后,Nginx err too many redirects 错误

我已经在 Google 上搜索了 6 个小时,但还是没找到我的 nginx.conf 有什么问题。我的设置是 django>gunicorn>nginx。我的 nginx.conf 如下(由于多次尝试,有点混乱):

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

我将非常感激您的帮助。谢谢

编辑以包含 curl 响应标头

* 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

主要问题是当通过 Chrome 浏览器访问域名时,它会返回“ERR:重定向次数过多”。这就是我想要解决的问题

答案1

您的 https 服务器包含以下行(摘录)。这告诉它将任何发往 https 站点的请求发送回 http 站点

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

您的 http 服务器包含这些行,将请求转发回 https 站点。

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

Nginx 正在执行您指示的操作,即创建重定向循环。解决方案取决于您要执行的操作。

相关内容