To redirect your website from HTTP to HTTPS using Nginx, follow these steps:
- Locate the Configuration File:
- For RHEL-based systems:
/etc/nginx/conf.d/default.conf - For Debian-based systems:
/etc/nginx/sites-enabled/default
- For RHEL-based systems:
- Identify the Server Block for Port 80:
- Use commands like
sudo nginx -T | grep -iw "configuration file"or look forincludedirectives to find the relevant configuration file.
- Use commands like
- Add the Redirect Directive:
- Open the identified server block in your preferred text editor.
- Ensure it is within the
serverblock listening on port 80. - Add the line
return 301 https://$server_name$request_uri;as the last directive in the block.
- Restart Nginx:
- After making changes, restart Nginx to apply the new configuration.
- Test the Redirect:
- Visit your site over HTTP to ensure it redirects correctly to HTTPS.
Example Configuration:
listen 80;
server_name your_domain.com www.your_domain.com;
return 301 https://$server_name$request_uri;
