•  Home
  •  Dashboard
  •  Company
    • About Us
    • Blog
    • Careers
    • Contact Us
    • Data Centers
    • Looking Glass
    • Network
    • Reseller
  •  Hosting Services
    • Infrastructure
      • iColocation
    • Compute
      • cMetal
      • cVirtual
    • Storage
      • sObject
      • sBlock
    • Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • Protection
      • pBackup
      • pDDoS
  •  Solutions
    • Ecommerce
    • Finance
    • Gaming
    • Hosting
    • Management
    • Security
    • System Integrator
  •  Support
    • Community
    • Knowledge Base
    • Open A Ticket
  •  USA & Canada: 800-933-1517
  •  International: 626-549-2801
  •  Email: sales@psychz.net
  • Services
    • new-colocation-header-img
      Infrastructure
      • iColocation
    • new-compute-header-img
      Compute
      • cMetal
      • cVirtual
    • new-storage-header-img
      Storage
      • sObject
      • sBlock
    • new-networking-header-img
      Networking
      • nCDN
      • nIP Transit
      • nWavelength
    • new-protection-header-img
      Protection
      • pBackup
      • pDDoS
  • Solutions
    • Ecommerce
    • Security
    • Gaming
    • Hosting
    • Management
    • Finance
    • System Integrator
  • Dashboard

nginx redirect http to https

  • Home
  • Client
  • Qa Forum
  • nginx redirect http to https

Posted By: Sargis | 1 Replies | Last Reply On: Jun 06, 2017 03:20:53

How to redirect https to https under nginx? 

Psychz - John

Votes: 0Posted On: Jun 06, 2017 03:20:53
 

Nginx is one of the world's most prominent web server solutions. It is an open source solution that provides high performance, concurrency and low memory consumption. Nginx also comes with a paid version called Nginx Plus.


Why redirect HTTP to HTTPS?

HTTP is the protocol with which the communication occurs between the web server and browser. HTTPS is another protocol used which is more secure than HTTP. The "S" stands for "secure". It uses a protocol such as SSL(Secure Sockets Layer) to encrypt data between web server and browser. Hence, it is very useful to redirect all HTTP data to HTTPS port. 


How to redirect HTTP to HTTPS?

Prerequisites

1. Setup HTTPS on Nginx - Firstly, you need to set up HTTPS on Nginx. For this, you will have to generate and private key and CSR(Certificate Signing Request) key.
2. After you have generated the key, the next step is to acquire the SSL certificate from a Certificate Authority. 
3. The third step is to configure Nginx server block by adding the SSL certificate and key. Reload the configuration to save the changes.


Redirecting all HTTP traffic to HTTPS

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

 

This a very general configuration that will redirect all the hostnames on the server. 

Let us go through the code line by line.

1. server - The name of the block in which the code is written.
2. listen 80 default_server; - The port number 80 is the "http" port and the default_server is the hostname of your server. This is to redirect all IPv4 HTTP packets incident on your server.
3. listen [::]:80 default_server; - This line functions same as above but for all IPv6 HTTP traffic.
4. return 301 https://$host$request_uri; - the code "301" is used to redirect the traffic. "https://$host$request_uri;" is the target at which all the traffic will be redirected to.

 

Here is an example for your reference.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name test.com www.test.com;
    return 301 https://$server_name$request_uri;
}

Here, all the HTTP traffic from "test.com" and "www.test.com" are redirected to HTTPS.

 

 

 

 

 

Was this reply helpful?

Related topics

  • How can I redirect an entire site to a single page?
  • Create CDN Redirections
  • How to fix HTTP 403 Forbidden Error Code?
  • Connection times out while being routed over your networks.
  • Nodejs https server
  • Reference Architecture (Active-Passive Configuration)
  • What are Redirects and Benefits of it
  • Someone from your IP trying to gain access to my vDSL modem?
  • Reading real IPs
  • Tools That Make Kubernetes Easier
  • The Difference Between Brotli And Gzip Compression Algorithms To Speed Up Your Site
  • Psychz CDN Products and Features Updates
  • CDN Hosting and why Every website should utilize a CDN
  • Top Encryption Software For Linux
  • Psychz Networks Moves into Seoul, South Korea to Launch A Data Center
Copyright © 2025 Psychz Networks,
A Profuse Solutions Inc Company
Hosting Services
Infrastructure
  • iColocation
Compute
  • cMetal
  • cVirtual
Storage
  • sObject
  • sBlock
Networking
  • nCDN
  • nIP Transit
  • nWavelength
Protection
  • pBackup
  • pDDoS
Company
  • About Us
  • Blog
  • Careers
  • Contact Us
  • Data Centers
  • Looking Glass
  • Network
  • Reseller
Policies
  • Acceptable Usage Policy
  • Privacy Policy
  • Service Level Agreement
  • Terms and Conditions
Support
  • Community
  • Knowledge Base
  • Open A Ticket
Get In Touch
  • Psychz Networks,
    A Profuse Solutions Company
    611 Wilshire Blvd #300
    Los Angeles,California 90017
    USA
  • US/Canada: 800-933-1517
  • International: 626-549-2801