mrparker
By:
mrparker

Nginx has redirect loop when trying to redirect to https

February 22, 2016 1.9k views
Nginx Ubuntu

example.com is my site's url.

It has a redirect loop on the line where it redirects to https.

Any fix?

Also how would I hide the PHP extension?

I have nginx 1.9.3 on ubuntu.

server {
    listen 80 default_server;
    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php;

    server_name example.com;
    return 301 https://example.com$request_uri; # Where it loops

    error_page 500 /500.html;
    error_page 404 /404.php;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;

        #htacess stuff

    }
}
1 Answer

Change

return 301 https://example.com$request_uri; to
return 301 https://$server_name$request_uri;

And where is your 443 listen? You can't point to https if you didn't set it up :)

  • I will try using Let's Encrypt because I don't have a SSL certificate to use with the site.

    Also I believe I have tried the suggested but without a ssl, I will try with an ssl this time.

  • Just made the changes and this is what I have:

    server {
        listen 443 ssl;
    
        server_name example.com www.example.com;
    
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
            ssl_prefer_server_ciphers on;
            ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    }
    
    server {
        listen 80 default_server;
    
        root /var/www/html;
    
        # Add index.php to the list if you are using PHP
        index index.php;
    
        server_name example.com;
    
        return 301 https://$server_name$request_uri;
    
        error_page 500 /500.html;
        error_page 404 /404.php;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    

    I followed the tutorial on adding a ssl cert from Let's Encrypt and it still has a redirect loop.

    This is the entire file, excluding all the comments.

    • maybe you should try to check /etc/nginx/conf.d/default.conf. There are some setting must be set up to use ssl.

  • Nevermind the config in the tutorial worked, it seemed to be a browser issue.

    Thanks for pushing me in the right direction!

    • You tried to redirect to https without having an ssl certificate xD ? Glad you got it working :)

      • It appears that the loop wasn't fixed, and at the time I was using cloudflare's ssl certificate.

        I generated my own just to test.

Have another answer? Share your knowledge.