This article describes how to support HTTP2 with your own services.

HTTP1

The problem of HTTP1

  1. Team head block

    If only one connection is used, it initiates the request and waits for the response before making the next request. If anything happens during a request reply, all remaining work is blocked after that request reply. This is “queue head blocking,” which blocks network traffic and Web page rendering until there is no response. To prevent this problem, modern browsers open up to six connections for a single domain and send requests through each connection. It achieves some degree of parallelism, but each connection is still subject to “queue head blocking”.

  2. Inefficient TCP utilization

    HTTP1 does not support multiplexing, and TCP guarantees that connections will work, but not that they will perform optimally.

  3. Bloated message headers

    While h1 provides a mechanism for compressing the requested content, the header of the message cannot be compressed.

  4. Restricted priority setting

    The browser can specify priorities in a limited number of ways: either initiate a request or not. Browsers defer requests for other resources in order to request higher-priority resources first. However, during the processing process, the browser does not initiate new resource requests, so the server cannot use this period of time to send resources with lower priorities, and the total page download time is prolonged.

  5. Third-party resources

    Many third-party resources are out of the control of Web developers, so it’s likely that some of them will perform poorly, delaying or even blocking page rendering.

HTTP1 optimization

The foundation is weak and the earth shakes. To optimize HTTP1, developers are using their ingenuity to the fullest.

Big names like Steve Souders, who wrote the High Performance Web Site Building Guide and a companion book, The Advanced Guide to High Performance Web Site Building, are the founding books of Web performance science.

Developers have also come up with a variety of practical solutions. Common Web performance optimization techniques are:

  1. DNS Query Optimization
  2. Tuning TCP connections (see my article on TCP Performance tuning)
  3. Avoid redirection
  4. Client cache
  5. Caching at the network edge
  6. Conditions of the cache
  7. Compression and code are extremely simple
  8. Avoid blocking CSS/JS
  9. Image optimization

There are also anti-pattern schemes

  1. Generate Sprite and resource merge/inline
  2. Domain name resolution
  3. The cookie domain name is disabled

We do all of this, no matter how troublesome how hard, mainly to optimize performance, HTTP1 to fill the pit we buried, although the symptoms not the root cause. Fortunately, HTTP2 is here.

HTTP2

HTTP2 advantages

  1. Binary protocol
    • HTTP/2 is in binary format rather than text format
    • It is convenient for machine analysis, but it is difficult for naked eye recognition
  2. HTTP/2 is fully multiplexed, not ordered and blocking
    • Parallelism can be achieved with only one connection
    • The TCP connection usage is improved
  3. The first compression
    • The HTTP/2 header is deeply compressed
    • Significantly reduce redundant bytes in transmission
  4. HTTP/2 allows the server to actively “push” responses into the client cache

HTTP2 deployment

Before deploying

Now let’s look at the result shown at www.asap2me.top/ before the upgrade

Chrome displays the protocol HTTP /1.1

The deployment of

Environment:

The current environment is Centos7.6

Web services: GoLang

Web service port number: 8080

Reverse proxy: Nginx

The system has configured Nginx and supports HTTPS. For details about how to install Nginx and configure HTTPS, see HTTPS Configuration

Deployment is divided into two steps:
  1. Get and install an H2-enabled Web server;
  2. Download and install a TLS certificate to connect the browser to the server over H2. There are two methods for applying for a TLS certificate
    • Using the online certificate generator www.sslchecker.com/csr/self_si…
    • Self-signed certificate OpenSSL tool
    • Let’s Encrypt

Since the certificate has been successfully installed, this section focuses on how Nginx supports HTTP2.

Nginx support HTTP2
  1. View the current module/usr/local/nginx/sbin/nginx – V
    • configure arguments: –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
  2. Go to /home/work/nginx-1.12.0
  3. Recompile to add the HTTPv2 module. ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_v2_module
  4. Make compiles, don’t do that heremake installOtherwise, it is overwrite installation.
    • The newly generated nginx is stored under objs of nginx source code
    • Make a backup of the old Nginx and replace the old nginx with the new nginx
  5. Listen 443 SSL http2;
  6. Sudo./nginx -s reload (kill the process if it doesn’t work, restart nginx)
Check the method
  1. Use curl to request something directly
curl --http2 -v https://www.asap2me.top -k
Copy the code

2. Use online resourcesMyssl.com/http2_check…

  1. Google Chrome request directly

Pressure test comparison

Performance using HTTP2 is not necessarily better than HTTP1, and sometimes depends on the business scenario. Like any performance tuning attempt, HTTP2 will likely undergo an iterative process of coding, testing, analysis, and optimization. Overall, with constant optimization, it will be better than HTTP1.

HTTP1 performance

Stress test http1 using ab:

ab -k -c 10 -t 180 -n 10000 www.asap2me.top/

Ab-k-c 10-t 180-n 10000 https://www.asap2me.top/ This is ApacheBench, Version 2.3 <$Revision: 1874286 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.asap2me.top (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: Nginx /1.12.0 Server Hostname: www.asap2me.top Server Port: 443 SSL/TLS Protocol: TLSv1.2, ecdhe-rsa-aes128-cm-sha256,2048,128 Server Temp Key: ECDH p-256 256 bits TLS Server Name: www.asap2me.top Document Path: / Document Length: 12 bytes Concurrency Level: 10 Time taken for tests: 30.967 seconds Complete Requests: 10000 Failed requests: 0 keep-alive requests: 9905 Total extension: 1749525 BYTES HTML transferred: 120000 Bytes Requests per second: 322.93 [#/ SEC] (mean) Time per request: 30.967 [MS] (mean) Time per request: 3.097 [MS] (mean, across all concurrent requests) Transfer rate: 55.17 [Kbytes/ SEC] Received Connection Times (ms) min mean[+/-sd] Median Max Connect: 0 1 9.5 0 121 Processing: 25 30 3.0 30 266 Waiting: 25 30 3.0 30 266 Total: 25 31 10.0 30 266 Percentage of the requests served within a certain time (MS) 50% 30 66% 30 75% 31 80% 31 90% 32 95% 33  98% 35 99% 115 100% 266 (longest request)Copy the code

HTTP2 performance

Stress test HTTP2 using H2Load:

h2load -n 10000 -c 6 -T 180 www.asap2me.top/

h2load -n 10000 -c 10 -T 180 https://www.asap2me.top/ starting benchmark... Spawning Thread # 0:10 Total Client (s).10000 total Requests TLS Protocol: TLSv1.2 Cipher: Spawning Thread # 0:10 Total Client (S).10000 Total Requests TLS Protocol: TLSv1.2 Cipher: ECDHE-RSA-AES128-GCM-SHA256 Server Temp Key: ECDH P-256 256 bits Application protocol: h2 progress: 10% done progress: 20% done progress: 30% done progress: 40% done progress: 50% done progress: 60% done progress: 70% done progress: Done finished in 31.71s, 315.37 req/s, 27.74KB/s requests: 10000 total, 10000 started, 10000 done, 10000 succeeded, 0 failed, 0 errored, 0 timeout status codes: 10000 2xx, 0 3xx, 0 4xx, 0 5xx traffic: 879.55KB (900660) total, 585.94KB (600000) headers (space savings 47.37%), 117.19KB (120000) Data min Max mean SD +/ -SD time for Request: 26.66ms 262.63ms 29.48ms 3.05ms 98.04% time for connect: 97.46ms 106.95ms 99.53ms 3.16ms 80.00% time to 1st byte: 127.59ms 141.29 MS 130.75ms 4.77ms 80.00% req/s: 31.54 35.41 33.82 1.14 80.00%Copy the code

There is little difference in total time between HTTP1.1 and HTTP2, mainly because the test interface is not suitable. However, we can still see significant improvements in machine CPU and traffic for HTTP2.

Pay attention to the problem

To upgrade a service to support HTTP2.0, consider the following:

  1. Browser support: Currently, all major browsers support H2. If they do not support H2, they will automatically revert to H1, so it is not a problem.
  2. Migrate to TLS: The server needs to support TLS 1.2 or higher, as well as a specific set of AD hoc encryption algorithms. Since most modern security-focused websites have been swept up in the “TLS everywhere” bandwagon, this shouldn’t be a problem.
  3. Undo the “optimizations” for HTTP/1.1: This part is the most work
  4. Third party resources: Third party resources can be a drag on any potential performance optimizations that HTTP/2 May bring, and you need to determine if you really need them.
  5. Support older clients: You need to ensure that TLS Settings and HTTP/1 support are still available, otherwise users may completely lose access to the site through H1

episode

In the process of writing the article, I suddenly reported this error, I thought there was a configuration problem, but later found that it was required to register.

If you buy the domain name in Tencent cloud, registration method:icp-faq.dnspod.cn/why

conclusion

It is recommended to deploy HTTP2 initially for new services and upgrade to HTTP2 for older services. For performance testing, the recommended web site is www.webpagetest.org/.

Writing a blog is a good thing. After writing this article, there are at least a few points worth writing:

  1. HTTP2 technical details
  2. Implementation of multiplexing
  3. Server push implementation
  4. Use webpagetest

The next article can write about a concrete implementation of multiplexing.

data

  1. HTTP2 Basics tutorial
  2. Why can’t I see the version number of the HTTP request in Chrome?
  3. Nginx configuration supports HTTP2
  4. Nginx supports the HTTP2 configuration process
  5. Nginx supports the Http2 protocol
  6. HTTP 2.0 PK HTTP 1.X — The Fast and the Furious
  7. Ab Command pressure test
  8. h2load – HTTP/2 benchmarking tool – HOW-TO
  9. Web Performance optimization Tool WebPageTest – Overview and configuration

The last

If you like my article, you can follow my public account (Programmer Malatang)

My personal blog is shidawuhen.github. IO /

Review of previous articles:

  1. The Go
  2. MySQL/Redis
  3. algorithm
  4. Architecture/network/project
  5. Think/read notes