How to Redirect HTTP to HTTPS Using Nginx

Author:Esadax
Updated:Jul 28, 2025

To redirect your website from HTTP to HTTPS using Nginx, follow these steps:

  1. Locate the Configuration File:

    • For RHEL-based systems: /etc/nginx/conf.d/default.conf
    • For Debian-based systems: /etc/nginx/sites-enabled/default
  2. Identify the Server Block for Port 80:

    • Use commands like sudo nginx -T | grep -iw "configuration file" or look for include directives to find the relevant configuration file.
  3. Add the Redirect Directive:

    • Open the identified server block in your preferred text editor.
    • Ensure it is within the server block listening on port 80.
    • Add the line return 301 https://$server_name$request_uri; as the last directive in the block.
  4. Example Configuration:

    listen 80;
    server_name your_domain.com www.your_domain.com;
    return 301 https://$server_name$request_uri;
    
  5. Restart Nginx:

    • After making changes, restart Nginx to apply the new configuration.
  6. Test the Redirect:

    • Visit your site over HTTP to ensure it redirects correctly to HTTPS.