Redirect HTTP to HTTPS using Nginx

In this post, I will share my very simple configuration to redirect HTTP to HTTPS using Nginx web server.

With my configuration below, Nginx will simply forwards port 80 to 443. Very useful when you have a service that only listens on 443 (https) then you can use Nginx to forward all of your HTTP requests to HTTPS (your actual service)

In nginx/conf/nginx.conf

worker_processes  1;
events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name host.saputra.org;
return 301 https://$host$request_uri;

access_log logs/host.access.log main;
}
}

I tested using Nginx v1.14.2 (Stable)
Download Link: .tar.gz | .zip

I hope this post is useful for you, if you have any questions or comments, feel free to post them down below.