Home / How to force redirection from HTTP to HTTPS in NGINX

How to force redirection from HTTP to HTTPS in NGINX

nginx

To redirect in NGINX, you only require the return directive and the 301 (Moved Permanently) status.

  • Redirecting HTTP to HTTPS per domain

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

  • Redirect any domain from HTTP to HTTPS.

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

  • Redirecting to a different domain:

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

Leave a Reply