How to redirect HTTP to HTTPS in Nginx

When I had installed Letsencrypt SSL, it was working fine. Some of the sites were automatically getting redirected from http to https however some some sites, it was not happening. Here is the server block code. Simply copy paste this before your existing server block and it will redirect http to https.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;

Please do let me know if this addressed your requirement.


Leave a Reply

Your email address will not be published. Required fields are marked *