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

Nginx realizes the display of the root domain name of the 301 jump to https

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of Nginx to jump to the root domain name of https, which is introduced in great detail through the sample code, which has a certain reference and learning value for everyone's study or work.

Based on the consideration of SEO and security, 301 jump is required. Nginx is used for general processing below.

Achieve the result

The following addresses need to be redirected to the root domain name of https

Https://chanvinxiao.comhttp://chanvinxiao.com (http without www) http://www.chanvinxiao.com (http with www) https://www.chanvinxiao.com (https with www) 301 and 302

301 is a permanent redirect and 302 is a temporary jump. The main difference lies in the way search engines treat it.

301: the search engine transfers weights and PR values

302: search engines will not do any extra processing.

Now I want the search engine to think that the original address no longer exists and transfer it to the new address completely, so use 301.

Http jumps to https

The easiest way is to return a redirected address directly in sever, with a 301 status code in the middle (otherwise it defaults to 302)

Server {listen 80; return 301 https://$host$request_uri;}

Both return and rewrite belong to the instructions of the rewriting module of Nginx, because there is no need to modify the path here, so it is more convenient to use return.

Both $host and $request_uri are embedded variables of the Nginx http module. Merging the two variables together is equivalent to removing the requested http://.

Www jumps to the root domain name

This only needs to be processed in https, because all http have jumped to https.

Server {listen 443 ssl; server_name ~ ^ (? www\.)? (. +) $; if ($www) {return 301 https://$2$request_uri;}.

Here, we make use of the regular matching function of server_name, which can be enabled by adding ~ before its value, and support PCRE syntax.

Regular is used to confirm whether there is a prefix www. And capture the root domain name to generate two variables, one is the named capture variable $www, and the other is the value capture variable $2

If does not support the use of sequence capture variables, otherwise it will report an error (unknown "1" variable), so it has been added? The value of $1 is assigned to $www

Reduce the number of jumps

The above settings have met the implementation results, but there is one flaw. Http://www.chanvinxiao.com will jump to https://www.chanvinxiao.com first, and then to https://chanvinxiao.com. The second jump is definitely not as good as the one-time jump, so it is best to make it directly in place in one step, and modify the configuration of http as follows:

Server {listen 80; server_name ~ ^ (?: www\.) (. +) $; return 301 https://$1$request_uri;}

In the sever corresponding to http, change server_name to regular mode and replace $host with the captured root domain $1

Www will be discarded directly here, so there is no need to capture, use?: tag to achieve only grouping and not capture, so the following root domain name becomes $1

The result is to jump to the root domain name of https without www, regardless of whether it is originally with www or not.

Summary

There is no need to specify a specific domain name in the above configuration, so it is easy to be compatible and migrated. The following features of Nginx are used:

Server_name regular matching return instruction receiving status code and address $host and $request_uri embedded variables

At this point, this is the end of this article on using Nginx to jump to the root domain name of https. For more information about Nginx 301 jumping to the root domain name of https, please search the previous article of script Home or continue to browse the relevant articles below. I hope you will support it in the future!

The original text is from: https://www.linuxprobe.com/nginx-301-https.html

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

Servers

Wechat

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

12
Report