NGINX – Redirecting HTTP to HTTPS

Tuesday, May 01, 2018

Using HTTPS is highly recommended and I cannot stop when explaining the benefits of using it! Also something which is more important is to make sure when HTTPS is setup it is ensured that all traffic via HTTP is blocked in a way that it is redirected to HTTPS. This can be easily achieved by giving NGINX the following set of instructions in the virtual server configurations.

Begin by setting up the virtual server for your application so that a domain name points to it. A simple virtual server configuration using SSL is as follows
server {
 listen 443 ssl;
 ssl_certificate /path/to/your/;
 ssl_certificate_key /path/to/your/;

 root /path/to/your/content;
 index index.html;
 include /etc/nginx/mime.types;
Now setup the redirect from HTTP to HTTPS using:
server {
 listen 80;
 return 301$request_uri;
this redirects to along with the URI in the request using the return directive and status code 301. For example, would be redirected to
This article was first published on the Knoldus blog.

You Might Also Like


Like us on Facebook

Featured Post

Beginners guide to Deep Web or Darknet

UPDATED  on 12th May 2018 The "Deep Web" or "Darknet" is a collection of un-indexed pages, meaning you can't fin...