NGINX – Redirecting HTTP to HTTPS


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;
 server_name example.com;
 
 ssl_certificate /path/to/your/example.com.crt;
 ssl_certificate_key /path/to/your/example.com.key;

 root /path/to/your/content;
 index index.html;
 include /etc/nginx/mime.types;
}

Other NGINX blogs you might find useful

  1. NGINX – Load Balancing your application made simple
  2. NGINX – Redirecting traffic between www and non-www domain
  3. NGINX – Restrict access to Geographical Locations using GeoIP module
  4. NGINX – Disable direct access (via http and https) to a website using IP address
  5. NGINX – Easiest way to setup SSL on using .pfx files

Now setup the redirect from HTTP to HTTPS using:

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

this redirects http://example.com to https://example.com along with the URI in the request using the return directive and status code 301. For example,

http://example.com/user/1 would be redirected to https://example.com/user/1.

knoldus-advt-sticker

This entry was posted in Devops and tagged , , , , . Bookmark the permalink.

5 Responses to NGINX – Redirecting HTTP to HTTPS

  1. Pingback: NGINX – Load Balancing your application made simple | Knoldus

  2. Pingback: NGINX – Redirecting traffic between www and non-www domain | Knoldus

  3. Pingback: NGINX – Restrict access to Geographical Locations using GeoIP module | Knoldus

  4. Pingback: NGINX – Disable direct access (via http and https) to a website using IP address | Knoldus

  5. Pingback: NGINX – Easiest way to setup SSL using .pfx files | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s