How to redirect nginx website from http to https


(Rushikesh Sonawane ) #1

I have Installed LEMP & WordPress and I was trying to install godaddy ssl certificate. I have done all process and there is no syntax error in server block. And you can see now ssl is on > https://www.sslshopper.com/ssl-checker.html?hostname=jankaribook.com

But unfortunately when I visit the domain(jankaribook.com ) It redirecting welcome to Nginx page. I am hundred percent sure there should be minor error in server block. Sound bad but I can’t figure out the problem. I am sharing below the server block of my Nginx. Plz check and tell me a correction.

 **_**guys Please give me a solution it might be last about setuping VPS**_**

server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name jankaribook.com www.jankaribook.com;
return 301 https://jankaribook.com$request_uri;
client_max_body_size 0;

server {
	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;

        server_name jankaribook.com;
 
        ssl on;
        ssl_certificate /etc/nginx/ssl/jankaribook.com.chained.crt;
        ssl_certificate_key /etc/nginx/ssl/jankaribook.com.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

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

location ~* \.php$ {
if ($uri !~ "^/uploads/") {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}

location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
expires 30d;
log_not_found off;
}
}

(Rushikesh Sonawane ) #2

@gulshankumar waiting for your replay.


#3

Try this one!

server {
	listen 80;
	root /var/www/html;
	index index.php index.html index.htm;
	server_name .jankaribook.com;
	client_max_body_size 0;

	error_page 404 /404.html;
	error_page 500 502 503 504 /50x.html;

	location ~* \.php$ {

		if ($uri !~ "^/uploads/") {

			fastcgi_pass unix:/run/php/php7.2-fpm.sock;
		}
		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_param SCRIPT_NAME $fastcgi_script_name;
	}

	location = /favicon.ico {

		log_not_found off;
		access_log off;
	}

	location = /robots.txt {

		log_not_found off;
		access_log off;
		allow all;
	}

	location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {

		expires 30d;
		log_not_found off;
	}

	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;

	server_name jankaribook.com;

	ssl on;
	ssl_certificate /etc/nginx/ssl/jankaribook.com.chained.crt;
	ssl_certificate_key /etc/nginx/ssl/jankaribook.com.key;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
	ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';


	server {

		if ($host = www.jankaribook.com) {

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


		if ($host = jankaribook.com) {

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


		listen 80;
		server_name .jankaribook.com;
		return 404;

	}


(Rushikesh Sonawane ) #4

Ok let me check and inform you back