Today, I will share the knowledge of domain name redirection and the practice of domain name redirection in CloudBase.
Pain points are born
Before, I developed a programming navigation website and put the website on Tencent Yunyun Development, which was deployed and maintained by cloud hosting (container). I also purchased a domain name code-nav.cn, and associated the sub-domain name www.code-nav.cn with the container storing website files in the background access service of cloud development. The configuration is shown in the following figure:
You can then access the website at www.code-nav.cn.
However, I soon found a serious problem.
Quite a few students wanted to visit my website, but the website could not be accessed because the address they entered was code-nav.cn and the prefix WWW was omitted. And I lost a lot of users.
First Battle – Domain name configuration
To solve this problem, in fact, it is very simple, before the WWW subdomain is configured to point to the container, then add a configuration in the cloud development background, directly purchase domain name (parent domain name) code-nav.cn also point to the container, not it?
The configuration is as follows:
In this way, users can access our website regardless of whether the WWW prefix is entered.
Looks like very perfect, but at present, the website actually still has certain problems.
Existing problems
First of all, with the WWW and without the WWW are actually two different urls, although to the user, it feels like visiting the same site. But for search engines, small spiders will identify them as two different websites, and separately included the content of these two paths, resulting in decentralized weight. Although the impact on small sites, but for large sites, this is a must to deal with the problem.
In addition, users visiting code-nav.cn (without WWW) reported that the data on the site could not be loaded. This is because the WEB security domain name developed by Tencent Yunyun limits that only domain names in the white list are allowed to access cloud resources (data, files, etc.). Therefore, it is necessary to add the code-nav.cn domain name in the security configuration.
Although the access is normal now, but if the business has some and WWW site strongly related logic, such as judging the user to visit the website must be www.code-nav.cn to allow login, then you have to modify the code, consider a little careless, will lead to some functional problems.
To solve these problems, we can use redirection techniques.
redirect
Redirection is a very broad concept, that is, various network requests are redirected to other locations through various methods, such as web page redirection, domain name redirection, data packet redirection, etc.
In the website development, there are too many application scenarios of redirection. For example, when a user does not log in, the url he enters will automatically jump to the login page. When users visit the old version of the website, it automatically redirects to the new version of the web page. Redirection is not only a tour guide, but also an overbearing security guard.
As a result, many large sites use redirection technology. For example, if you go to Google.com and press F12 to view the developer console, you can see that the url is automatically redirected to www.. Com.
Which begs the question, what is a 302 redirect?
Take a look at the common HTTP status codes for redirection.
Redirects the HTTP status code
The main HTTP status codes related to redirection are 301, 302, 303, 307, 308. The most commonly used are 301 and 302.
Moved Permanently: Indicates that the requested resource has been Permanently Moved Permanently to the URL specified by the Location header. It is fixed and will not change Permanently, and the search engine will modify it based on the response.
The 302 is Moved Temporarily (Moved Temporarily, or Found), indicating that the requested resource was Temporarily Moved to the URL specified by the Location header. The browser redirects to the URL, but the search engine does not update the link to the resource.
Although both 301 and 302 can change the url A entered by the user to the redirected URL B, they are different:
- Search engine difference: 301 indicates that the resource at the original address A has been removed and cannot be accessed forever. When the search engine captures the content, it will replace all the url A with B. 302 indicates that site A is still alive, and the search engine will keep A record of site A while capturing the new content of site B.
- Security: 302 redirect has the risk of website hijacking, resulting in website theft.
Battle Again – Cloud development redirection practices
Now that you know about redirection, see how to implement it and how to implement domain name redirection in cloud development.
Depending on the Web server you use, such as Nginx, Apache, Tomcat, etc., redirection can be implemented in a number of ways, usually by adding a few configurations to the server.
My programmatic navigation site is deployed as a container on cloud hosting provided by cloud development. I packaged the developed web site files together with the Nginx server that provides web services into containers, so that each container can be treated as a small server and run independently.
To support redirection, you only need to modify the Nginx configuration. For example, here I choose to add 301 permanent redirection to the entire site, and the configuration file is as follows:
server { listen 80; # gzip config gzip on; . root /usr/share/nginx/html; include /etc/nginx/mime.types; Rewrite ^(.*) https://www.code-nav.cn permanent; rewrite ^(.*); }}Copy the code
Instead of writing and memorizing the Nginx configuration, use the visual interface to generate it:
See this article for details: Nginx configuration Code with Ease! Other server configurations are also self-documented and will not be covered here.
Then, create a new version on cloud hosting, release a new container, and you’re done!
To check out the effect, go to code-nav.cn and the site is redirected to www.code-nav.cn. Perfect!
Finally, review the complete process of domain name redirection in Tencent Cloud development, including the following steps:
- Add the parent domain name to the site in the access service (cloud hosting, etc.)
- Add the parent domain name to the whitelist in the security configuration
- Add the weighting configuration to the Web server
- Create a new version and deploy it
The whole process is very simple ~ students who need to have a try!
Product introduction
Cloud Development (TCB) is a cloud native integrated development environment and tool platform provided by Tencent Cloud. It provides developers with highly available, automatic and flexible expansion of back-end cloud services, including computing, storage, hosting and other serverless capabilities, which can be used for cloud integrated development of various end applications (small programs, Public account, Web application, Flutter client, etc.) to help developers build and manage back-end services and cloud resources in a unified manner, avoiding tedious server construction, operation and maintenance during application development. Developers can focus on the implementation of business logic, with lower development threshold and higher efficiency.
Open cloud development: console.cloud.tencent.com/tcb?tdl_anc…
The product documentation: cloud.tencent.com/product/tcb…
Technical documentation: Cloudbase.net? From =10004
Technical exchange group, latest information follow wechat public account [Tencent Yunyun Development]