Question

Need help fixing http to https on LEMP

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;
        }

}


Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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;
    }
}