Can't figure out, how to redirect http to https, on an Nginx vhost

Discussion in 'Server Operation' started by alleks, Feb 17, 2015.

  1. alleks

    alleks Member

    I've tried almost all, if not all, the results I could find, to implement a redirect from http://xyz.com to https://xyz.com, and all ended in failure, with an "Internal loop redirect error" message.

    Any tips on how to achieve this redirect?

    This is my actual configuration for the site:
    • domain: xyz.com
    • auto-subdomain: www.
    • ssl: CHECK
    • php: php-fpm
    • redirect: SEO Redirect - www to non-www
    • custom nginx directives
    Code:
    location / {
        try_files $uri $uri/ /index.php?/$request_uri;
        rewrite ^/(en|es|ro)/(.*)$ /$2 permanent;
    }
    Any help, would be much appreciated.
     
  2. ressel

    ressel Member

    Try following.
    Code:
    server {
           listen         80;
           server_name    my.domain.com;
           return         301 https://$server_name$request_uri;
    }
    
    server {
           listen         443 ssl;
           server_name    my.domain.com;
    
           [....]
    }
     
  3. alleks

    alleks Member

    That was the first thing I have tried, and it resulted in the same error.
     
  4. Nap

    Nap Member

    Using apache2, this is how it's done with phpMyAdmin:

    In apache2/conf-available, there is a symlink to the phpMyAdmin config file (/etc/phpmyadmin/apache.conf). In apache2/conf-enabled (because phpMyAdmin is enabled), there is a symlink back into conf-available.
    (This probably doesn't matter for your question, but I thought I would include it anyway for others.)

    In the apache.conf file, it has, at the very top:
    Code:
    <IfModule mod_rewrite.c>
      <IfModule mod_ssl.c>
      <Location /phpmyadmin>
      RewriteEngine on
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
      </Location>
      </IfModule>
    </IfModule>
    
    So, it's not actually redirecting, but rather using a rewrite rule.
    When I try to access phpMyAdmin using http, the request is converted to https.

    I'm not using NGINX on my server, but hope this helps anyway.

    Cheers,
    Nap
     
  5. alleks

    alleks Member

    Nap, why would you think that would help? I'm not using Apache.
     
  6. till

    till Super Moderator Staff Member ISPConfig Developer

    I use this for nginx:
    Code:
    if ($scheme != "https") {
              rewrite ^ https://xyz.com$request_uri? permanent;
            }
     
  7. alleks

    alleks Member

    One of the first things I tried, if not the first... and I still get internal loop redirection issue
     
  8. till

    till Super Moderator Staff Member ISPConfig Developer

    The above works, you can see that here at howtoforge when you try to use the site without https.

    You will get a redirect loop when you use:

    if ($scheme != "https") {
    rewrite ^ https://www.xyz.com$request_uri? permanent;
    }
    instead of the one I posted above.

    Another possibility is that the vms or shop that you use in your website redirects back to http and therefor causes this loop.
     
  9. Nap

    Nap Member

    I suspected a rewrite was needed and thought I would post how I've done it, in case NGINX was similar.
     
  10. alleks

    alleks Member

    Thanks for trying to help Nap, but it's slightly different.

    Til, I was aware of the www. but, it still doesn't work. Same issue. I just don't know where else to look... so confusing
     
  11. alleks

    alleks Member

    Ok, I got the issue fixed.

    It was because my Cloudflare SSL setting was set to Flexible SSL and I've changed it for Strict SSL and I've added this nginx rule to my domain:

    Code:
    if ($http_x_forwarded_proto = "http") {
                return 301 https://$server_name$request_uri;
            }
    
     

Share This Page