Nginx – Redirect loop when forcing SSL with Nginx

301-redirectnginxredirectssl

I know this question has been asked and answered a million times, but none of the accepted answers are working for me. My site is unreachable due to some sort of infinite redirect loop when I force all connections to https.

root /usr/share/nginx/html/flawedspirit.com/root_html;
index index.php index.html index.htm;

error_page 403 /index.php;
error_page 404 /error/404.php;
error_page 500 502 503 504 /error/50X.php;

access_log /usr/share/nginx/html/flawedspirit.com/logs/access.log;
error_log /usr/share/nginx/html/flawedspirit.com/logs/error.log;

server {
    listen 80 default_server;
    server_name flawedspirit.com;
    return 301 https://$server_name$request_uri;

    location ~ \.php {
        try_files $uri $uri/ =404;
        include fastcgi_params;
        fastcgi_pass php5-fpm-sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }

    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|woff|ttf|svg)$ {
        expires max;
        log_not_found off;
    }
}

server {
    listen 443 ssl;
    server_name flawedspirit.com;

    ssl on;
    ssl_certificate <...>;
    ssl_certificate_key <...>;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM";
    ssl_prefer_server_ciphers on;
    ssl_ecdh_curve secp521r1;

    location ~ \.php {
        try_files $uri $uri/ @no-extension;
        include fastcgi_params;
        fastcgi_pass php5-fpm-sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
        fastcgi_intercept_errors on;
    }

    location @no-extension {
        rewrite ^(.*)$ $1.php last;
    }

    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|woff|ttf|svg)$ {
        expires max;
        log_not_found off;
    }
}

So what am I doing wrong?

Best Answer

Ok, so maybe it WAS a caching issue, just not in the first place I expected. I went to Cloudflare and nuked my site's cache there, and set my SSL setting to Full. That seemed to do it.

EDIT: I can't choose this as the answer for 48 more hours, so consider this the answer.