As one of the best web servers in the world, the advantages of Nginx are self-evident. Let’s talk about how Nginx merges request connections.
tips
When we browse the web, an important factor affecting the browsing speed is the number of concurrent browsers. The number of concurrency is simple and popular, that is, the number of simultaneous work when browsing the webpage.
Of course, the browser’s limit on the number of concurrent requests is for the same domain name, and there is a certain limit for the number of requests under the same domain name at the same time, and requests exceeding the limit number will be blocked.
First, let’s look at the number of concurrent connections of each browser:
List the possible considerations of the browser’s decision
-
Due to the limitation of the TCP protocol, only 65536 ports on the PC side can be used to send connections to the outside, and the operating system also has a limit on the number of half-open connections to protect the TCP\IP protocol stack resources of the operating system from being exhausted quickly, so the browser does not It is best to issue too many TCP connections, but to reuse the TCP connection after running out or simply re-establish the TCP connection.
-
If you use the blocking socket model to establish a connection, sending out multiple connections at the same time will cause the browser to open several more threads, and threads are sometimes not considered lightweight resources. After all, the overhead of doing a context switch is not small.
-
This is the browser protecting the server as a conscientious client. Just like the conflict detection mechanism of Ethernet, the client must decide a waiting period when using public resources. When more than 2 clients want to use a common resource, the strong evil client may cause the weak client to completely lose access to the common resource. In the past, Xunlei was criticized because it was not a conscientious client. As an HTTP protocol client, it did not consider the pressure on the server, and as a BitTorrent client, it did not consider its obligation to repay uploads.
Taobao tengine
When we visit some websites, we will see that some js and css files in the code are obtained through one request. The above knowledge knows that the number of concurrent browser requests is limited, but if multiple records are merged once request may speed up the response time.
The tengine used by Taobao is an nginx-based web server, which has been open-sourced since the end of 2011. The open source module nginx-http-concat can combine multiple files in one response message.
Install
Install the third-party module nginx-http-concat for the first time
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz wget https://github.com/alibaba/nginx-http-concat/archive /master.zip -O nginx-http-concat-master.zip unzip nginx-http-concat-master.zip tar -xzvf tengine-2.2.0.tar.gz cd tengine-2.2.0
Configure, compile and install
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx -http-concat-master makemake install
If Nginx has already been installed, just configure third-party plug-ins. Nginx -V, check the version of Nginx, if not, download the corresponding version.
Just execute the code.
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx -http-concat-master make
After successful compilation
#Please backup nginx execution file before copying cp /objs/nginx /usr/local/nginx/sbin/nginx
Kill Nginx and restart it.
configuration
Add the following configuration in the location section:
location /static/css/ { concat on; concat_max_files 20; concat_unique off; concat_types text/ css application/Javascript; }
Merge method
http://static.52itstyle.com/static/css/? ?index.css,common.css?v=20171111