title: NGINX redirect http to https is not working
body: <p>I was trying to set up a wordpress web server using nginx on my own laptop. The certificate was installed successfully and I can visit my site through https. However, I found out that the redirecting from http to https is not working. My site always showed <strong>400 Bad Request</strong> when I tried to visit through http. Lots of answers on Google say that this can be fixed by commenting out &quot;ssl on&quot; but actually I don't have this line in my code.</p>
  <pre><code>server {
      root /var/www/html;
      index index.php index.html index.htm;
      server_name mydomain.com;
  
      location / {
              try_files $uri $uri/ =404;
      }
  
      location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
      }
  
      location ~ /\.ht {
              deny all;
      }
  
  listen 443 ssl; # managed by Certbot
  ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot             
  }
  
  server {
      if ($host = mydomain.com) {
      return 301 https://$host$request_uri;
  
      } # managed by Certbot
  
  
      listen 80 default_server;
      server_name mydomain.com;
      return 404; # managed by Certbot
   }
  </code></pre>