•  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的HTTP重定向到https

  • Home
  • Client
  • Qa Forum
  • nginx的HTTP重定向到https

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

如何HTTPS重定向到https下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

  • 開源網絡映射軟件
  • 服務器託管在歐洲
  • 最佳開源軟件對於Linux用戶
  • 尋找256IPs專用服務器
  • 全局服務器負載平衡
  • 事情要考慮當選擇一個加密解決方案
  • sObject - Buckets
  • sBlock - Snapshots Create
  • Why Gaming Companies are Choosing Bare Metal Servers
  • Unique Visitors
  • How To Run Kubernetes on Psychz Networks Bare Metal Servers
  • Hosted Server For Small Business
  • Does Psychz offer 30 day Money Back Guarantee?
  • DFW Colocation
  • CDN and Load Balancer: Ultimate combination for efficient content delivery
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