Need help fixing http to https on LEMP

June 23, 2019 1.6k views
Nginx CentOS

Enabling return 301 https://dom1.com$request_uri; line makes too many redirects. A also tried 2 blocks with one for 80 2nd for 443 same error.

Here is my config for vhost file:

server {
        listen 80;
        listen          443 ssl http2;
        server_name     dom1.com www.dom1.com;
#   return 301 https://dom1.com$request_uri;



        pagespeed off;

        # use any of the following two
        real_ip_header CF-Connecting-IP;

        #Root Folder
        root   www/dom1.com;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
        ssl_buffer_size 8k;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:50m;
        ssl_session_timeout 30m;

        #ssl_certificate /var/lib/acme/live/dom1.com/fullchain;
        ssl_certificate /var/lib/acme/live/dom1.com/fullchaini_new;
        #ssl_certificate_key /var/lib/acme/live/dom1.com/privkey;
        ssl_certificate_key /var/lib/acme/live/dom1.com/privkey_new;

        ssl_dhparam /usr/local/nginx/conf.d/dhparams.pem;

        ssl_stapling on;
        resolver 8.8.8.8;
        ssl_stapling_verify on;
        ssl_trusted_certificate /var/lib/acme/live/dom1.com/fullchain;

        location / {
                index  index.php index.html;
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        include /usr/local/nginx/www/dom1.com/nginx.conf;

        #Static Files Caching
        location ~ \.(css|less|js|gif|png|jpeg|jpg|ico|woff|woff2)$ {
                expires 31536000s;
                add_header Pragma "public";
                add_header Cache-Control "max-age=31536000, public";
        }



        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
        }

}


2 comments
  • Hiya,

    It looks like you are serving both the port 80 and port 443 on the same virtual server. This means that even when the user makes a 443 request, they are getting the 80 -> 443 rule, which puts them in an infinite loop. You should be able to fix this by having a second virtual server for port 80 that just contains the 80 -> 443 redirect.

  • Hey thanks for the response. Yes as I mentioned this works fine from 3 years but when I'm doing 2 blocks of server it shows loop error.

    Why I need to change now: Unbale to fix http to https redirect with this.

    Enabling return 301 https://dom1.com$request_uri; also doesnt fix (give loop error)

1 Answer
jtittle MOD June 26, 2019
Accepted Answer

@dmohanty

When it comes to NGINX, ideally you'll want to separate the server blocks--one for serving requests on port 80 (which redirects to 443) and one for port 443.

Instead of using the domain, I would recommend using $host combined with $request_uri.

Additionally, looking to your configuration, I see that you are pulling the CloudFlare IP. Since CloudFlare natively provides SSL, you'll want to make sure you're set to use Strict mode so that you avoid the infinite loop.

It may take a few minutes for the Strict mode to take effect, and you may need to fully clear your browser cache before you'll be able to utilize the new 301 redirect.

Example (using the configuration you provided)

server {
    listen 80;
    server_name domain.com www.domain.com;

    #
    # No additional configuration needed. This block only exists to redirect
    # requests to port 443 (SSL).
    #

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

server {
    listen 443 ssl;
    server_name domain.com www.domain.com;

    root /var/www/domain1.com;

    real_ip_header CF-Connecting-IP;

    #
    # SSL Configuration Goes Here
    #
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    ssl_buffer_size 8k;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 30m;
    ssl_certificate /var/lib/acme/live/dom1.com/fullchaini_new;
    ssl_certificate_key /var/lib/acme/live/dom1.com/privkey_new;
    ssl_dhparam /usr/local/nginx/conf.d/dhparams.pem;
    ssl_stapling on;
    resolver 8.8.8.8;
    ssl_stapling_verify on;
    ssl_trusted_certificate /var/lib/acme/live/dom1.com/fullchain;

    location / {
        index  index.php index.html;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    include /usr/local/nginx/www/dom1.com/nginx.conf;

    #
    # Static Files Caching
    #
    location ~ \.(css|less|js|gif|png|jpeg|jpg|ico|woff|woff2)$ {
        expires 31536000s;
        add_header Pragma "public";
        add_header Cache-Control "max-age=31536000, public";
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /usr/local/nginx/conf/fastcgi_params;
    }
}
  • Thanks for your valuable time. I have also tried without full domain and with $request_uri and without cloudflare.

    I think when I'm not using Cloudflare I need to disable "realipheader CF-Connecting-IP;" ?

    I will try and update the results soon.

Have another answer? Share your knowledge.