Nginx – rewrite HTTP to HTTPS

A simple code snippet that can be used to rewrite HTTP request to HTTPS.

Redirect every website to HTTPS.

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

Redirect specific website.

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

Sources

Nginx Pitfalls – Taxing Rewrites