Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods that nginx enforces https access?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "what are the methods for nginx to force https access". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the methods that nginx enforces https access?"

Rewrite method of nginx

Train of thought

This should be the easiest way for everyone to rewrite all http requests to https via rewrite.

Configuration

Server {listen 111 listen 80; server_name testcom; rewrite ^ (*) $https://$host$1 permanent;}

After building this virtual host, you can rewrite all the requests of http://test.com to https://test.com.

497 status code of nginx

Error code 497

497-normal request was sent to https

Explanation: when only https is allowed to access this virtual site, nginx will report a 497 error code when accessing with http

Train of thought

Use the error_page command to redirect the link of the 497 status code to the domain name https://test.com

Configuration

Server {listen 11 listen 443; # ssl port 11 listen 80; # users are accustomed to access it with http, plus 80, and then use the status code to make it automatically jump to port 443 server_name testcom; # to enable a server {} to support ssl on; # specify a certificate file in pem format ssl_certificate / etc/nginx/testpem; # specify a private key file ssl_certificate_key / etc/nginx/testkey in pem format # redirect http requests to https requests error_page 497 https://$host$uri?$args;}

Index.html refreshes the web page

Train of thought

Both of the above methods will consume the resources of the server. Let's visit baidu.com with curl to see how Baidu's company can jump from baidu.com to www.baidu.com.

We can see that Baidu skillfully uses the refresh function of meta to jump baidu.com to www.baidu.com. Therefore, we can also write an index.html under the virtual host path based on http://test.com, which is the jump from http to https.

Index.html

Nginx virtual host configuration

Server {listen 11server_name testcom; location; server_name testcom; location / {# indexhtml under the root directory of virtual host listening root / srv/www/httptestcom/;} # redirect the page of 404 to the home page of https error_page 404 https://testcom/;} so far, I believe you have a deeper understanding of "what methods nginx forces to use https access", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report