Nginx enforces HTTPS access (HTTP redirects to https)

Source: Internet
Author: User
Tags virtual environment
Requirements

Built a virtual host for HTTPS access based on nginx, the listening domain name is test.com, but many users do not know the difference between HTTPS and HTTP, it is easy to knock into the http://test.com, in this case, the Error 404 is reported, so I needHTTP force redirect to HTTPS Based on test.com Domain Name

I have summarized three methods and shared them with you.

Nginx rewrite Method

This should be the easiest way to think of. You can simply rewrite all HTTP requests to HTTPS through rewrite.
server {listen192.168.1.111:80;server_nametest.com;rewrite ^(.*)$https://$host$1permanent;}

After setting up this Vm, you can rewrite all requests from the http://test.com to the nginx 497 status code on the https://test.com.

Error Code 497
497 - normal request was sent to HTTPS

Explanation: When this virtual site only allows HTTPS access, nginx will report a 497 error code idea When HTTP access is used. Use the error_page command to redirect the 497 status code link to the https://test.com domain name for configuration

Server {Listen 192.168.1.11: 443; # SSL port listen 192.168.1.11: 80; # the user is used to HTTP access and 80 is followed by a 497 status code to automatically jump to port 443 SERVER_NAME test.com; # For a server {......} enable SSL to support SSL on; # specify the Certificate file ssl_certificate/etc/nginx/test in PEM format. PEM; # specify the private key file ssl_certificate_key/etc/nginx/test. key; # redirect an HTTP request to an HTTPS request error_page 497 https: // $ host $ Uri? $ ARGs ;}

Index.html

The above two methods consume server resources. Let's use curl to access Baidu.com, looking at how Baidu's company implemented the jump from Baidu.com to www.baidu.com, we can see that Baidu cleverly uses Meta to refresh and redirects Baidu.com to www.baidu.com. therefore, we can also write an index.html based on the virtual environment of the http://test.com, the content of httpto the https的 index.html

Nginx Virtual Host Configuration

Server {Listen 192.168.1.11: 80; server_nametest.com; Location/{expose index.html is placed under the root directory of the VM listener root/srv/www/http.test.com /;} # redirect the 404 page to the HTTPS homepage error_page404https: // test.com /;}
Note (1) the above three methods can force redirect HTTP requests to HTTPS requests based on nginx. You can evaluate the advantages and disadvantages or select based on your actual needs. (2) My blog is intended for students with a certain degree of foundation. I have deleted many configurations based on project security considerations based on my actual situation, Do not be brainless!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.