Recently, a classmate asked me why the same website should place the main page and static resources (pictures, etc.) under different domain names (the same server may be parsed, or it may be different). What is the purpose of doing this? Let's listen slowly~
1. Breakthrough browser concurrency limit
Browsers have a limit on the number of concurrent resource requests within the same domain name ( in order to prevent DDOS attacks . ) , and different browsers have different numbers of concurrent loads of static resources under the same domain name. If the maximum limit is exceeded, the excess requests will be blocked . The browser will wait for the previous resources to be downloaded, and then reuse the tcp connected earlier to initiate subsequent requests.
The following are the number of concurrent requests for some common browsers:
Chrome34/32 | 6 |
IE10 | 8 |
IE11 | 13 |
Firefox27/26 | 6 |
Safari 7.0.1 | 6 |
It is conceivable that if we put the static resources on another domain name, which is different from the main page, we can break through the browser's limit on the number of concurrent requests and reduce the loading time of the webpage. It can not only increase the number of simultaneous requests , but also reduce the requests under the main domain name , so as to achieve the effect of reducing the white screen time of the web page or the DCL and L time.
2. Save cookie bandwidth
The cookie is followed by the domain name. All requests under the same domain name will carry cookies . It is conceivable that if all our resources are under one domain name, then loading those static resources will also bring cookies, which obviously increases the request cost.
In most cases, requests for static resources do not need to carry cookies, such as browsing pictures, videos and other information. Unlike the main domain name, cookies will not be carried automatically , so we can avoid carrying cookies by placing static resources on another domain name, saving request bandwidth.
3. More convenient CDN caching
Storing static content and dynamic requests on different servers makes it easier to CDN
request.
4. Domain name convergence
- domain divergence
Domain name divergence refers to placing static resources under multiple sub-domain names, so that multi-threaded downloads can be performed, which improves parallelism and enables clients to load static resources more quickly. That is, the content discussed in this article, the main page and static resources should be placed under different domain names
- domain name convergence
Domain name convergence refers to placing static resources under one domain name. Reduce DNS resolution overhead.
Since it takes time for browsers to resolve DNS, too many domain names will lead to long resolution time and slow access speed.
So we need to control the number of domain names that appear on the page. Generally speaking, the main site + CDN two domain names are more reasonable.