Skip to content
TrySitePrice

HowTo: Nginx Redirect HTTP To HTTPS with Rewrite 301 Rules

Best/Tutorial

To redirect HTTP traffic to HTTPS in Nginx, you can add the following rewrite rule in the server block of your Nginx configuration file:

server {
 listen 80;
 server_name example.com;
 return 301 https://$host$request_uri;
}

Replace example.com with your domain name.

This rule will listen for incoming HTTP traffic on port 80, and for all requests, it will return a 301 permanent redirect to the same URL with HTTPS.

Make sure you have a server block configured for HTTPS traffic and that you have a valid SSL certificate installed.

You can then restart Nginx to apply the changes:

sudo service nginx restart