Skip to content

Nginx Redirect HTTP to HTTPS

How to Nginx Redirect HTTP to HTTPS. This tutorial will help you redirect all incoming request on http to https url in Nginx web server.

Category
Web Servers
Covers
Apache, Nginx, SSL/TLS
On this page

Nginx is an open-source, high-performance HTTP and reverse proxy server. This has become popular among many hosting providers. Everyone knows that transferring private data like credentials, payment information over insecure protocol is not secure. They can easily be sniffed by a MITM attacker. This tutorial will help you redirect incoming HTTP traffic to HTTPS in the Nginx web server.

Redirect ALL to HTTPS

This will redirect all requests hits to port 80 except domains with separate server blocks. To do this edit virtual host configuration file for your domain and add “return 301 https://$host$request_uri” statement under server section. This will redirect all the incoming requests on HTTP to corresponding https URLs.

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

Redirect Specific Domain

You may want to redirect a specific domain to HTTPS. Use the following configuration on Nginx to redirect all HTTP requests on tecadmin.net to HTTPS.

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

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top