使用nginx的ERR_TOO_MANY_REDIRECTS

时间:2017-09-25 20:16:01

标签: redirect nginx https

我想将我的所有http流量重定向到重定向到https。我正在使用letsencrypt。我在线阅读return 301 https://$server_name$request_uri;会将我网站的所有流量重定向到https,但会导致ERR_TOO_MANY_REDIRECTS

没有上述声明,一切正常,但我必须在URL中专门指定https。这是我的/etc/nginx/sites-available/default文件:

server {
        listen 80 default_server;
        listen 443 ssl default_server;

        ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;

        root /home/website/mywebsite/public;

        index index.html index.htm index.php;

        server_name mywebsite.me www.mywebsite.me;

        return 301 https://$server_name$request_uri;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        }
}

我哪里错了?

3 个答案:

答案 0 :(得分:17)

在我的情况下是Cloudflare。我不得不更改为完全SSL加密

答案 1 :(得分:3)

将配置更改为以下

server {
        listen 80 default_server;
        server_name mywebsite.me www.mywebsite.me;

        return 301 https://$server_name$request_uri;
}

server {

        listen 443 ssl default_server;

        ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;

        root /home/website/mywebsite/public;

        index index.html index.htm index.php;

        server_name mywebsite.me www.mywebsite.me;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        }
}

您当前的配置会将http和https重定向到https。因此,由于return语句,它变成了无限循环。只有在连接为http时才需要return语句。所以你将它分成两个服务器块

答案 2 :(得分:0)

我有同样的问题。我发现了很多关于它的问题/答案和文章,但没有任何帮助。 我然后尝试从不同的浏览器访问我的网站,它工作得很好! 从我的Chrome浏览器中删除缓存解决了它。

所以 - 当您尝试使用不同的解决方案来清除缓存时请记住