Apache http2

最後更新: 2023-10-15

http2 好處

  • binary protocol
  • header compression
  • Multiplexing & Full-Duplexing
  • server push

Install

 

# Ubuntu 14.04.5

apache2-bin 已自帶了 mod_http2.so (/usr/lib/apache2/modules/mod_http2.so)

# Centos 7 (20181015)

要安 IUS 包才用到 http2

 


注意

 

Apache 2.4.27, HTTP/2 not supported in prefork

To fix this, select a different MPM: event(highly recommend) or worker.

/etc/httpd/conf.modules.d/00-mpm.conf

LoadModule mpm_event_module modules/mod_mpm_event.so

 


Overhead

 

 * Enabling HTTP/2 on your Apache Server has impact on the resource consumption and if you have a busy site ( threads & memory )

HTTP/2 gives all requests that it receives to its own Worker threads for processing,
collects the results and streams them out to the client.

More state on the server to manage all the open request, priorities for and dependencies between them

 


Enable http2

 

LoadModule

LoadModule http2_module modules/mod_http2.so
<IfModule http2_module>
    LogLevel http2:info
</IfModule>

Enable HTTP/2 via Protocols

The HTTP/2 protocol does not require the use of encryption so two schemes are available:

h2(HTTP/2 over TLS)

Allows HTTP/2 negotiation (h2) via TLS ALPN for secure <VirtualHost>

h2c(HTTP/2 over TCP) <-- 沒有人使用

Allows HTTP/2 cleartext negotiation (h2c) upgrading from an initial HTTP/1.1 connection

c = Cleartext

i.e.

# TLS only
Protocols h2

# The order of protocols mentioned is also relevant.
# By default, the first one is the most preferred protocol

# TLS http2, cleartext http2 & http/1.1
Protocols h2 h2c http/1.1

# Depending on where you put this directive,
# it affects all connections or just the ones to a certain virtual host.

Protocols http/1.1
<VirtualHost ...>
    ServerName test.example.org
    Protocols h2 http/1.1
</VirtualHost>

 


Configure

 

H2MaxSessionStreams (default is 100)

sets the maximum number of active streams(parallel requests) per HTTP/2 session

H2MaxWorkers / H2MinWorkers

maximum number of worker threads to spawn per child process for HTTP/2 processing.
(Default: mod_http2 will chose a value suitable for the mpm module loaded. )

H2StreamMaxMemSize 65536

controls how much response(outgoing) data shall be buffered.
If the client does not read fast enough,
the connection will buffer this amount of data and then suspend the H2Worker.

H2WindowSize 65535

flow control from client to server and limits the amount of data the server has to buffer.
The client will stop sending on a stream once the limit has been reached until the server announces more available space
This limit affects only request bodies, not its meta data such as headers.

H2WindowSize

controls how much the client is allowed to send as body of a request
The client will stop sending on a stream once the limit has been reached
until the server announces more available space

H2Direct

Default: on for h2c, off h2 protocol

On: if the first bytes received by the server on a connection match the HTTP/2 preamble,

       the HTTP/2 protocol is switched to immediately without further negotiation.

H2Upgrade

Default: on for h2c, off for h2 protocol

This directive toggles the usage of the HTTP/1.1 Upgrade method for switching to HTTP/2.

(uses the "Upgrade" header to announce willingness to use another protocol)

This should be used inside a <VirtualHost> section to enable Upgrades to HTTP/2 for that virtual host.

Upgrades are only accepted for requests that carry no body.

POSTs and PUTs with content will never trigger an upgrade to HTTP/2

 


Test

 

server log

[Mon Oct 15 17:38:26.052244 2018] [http2:info] [pid 24236] AH03090: mod_http2 (v1.10.20, feats=CHPRIO+SHA256+INVHD+DWINS, nghttp2 1.31.1), 
  initializing...
[Mon Oct 15 17:38:26.052306 2018] [http2:warn] [pid 24236] AH10034: 
  The mpm module (prefork.c) is not supported by mod_http2. 
  The mpm determines how things are processed in your server. 
  HTTP/2 has more demands in this regard and the currently selected mpm will just not do. 
  This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.

curl

curl -V | grep --color HTTP2

curl -v --http2 http://xxx |& grep grep ALPN

* ALPN, offering h2
* ALPN, offering http/1.1
* ALPN, server accepted to use h2

chrome

chrome://net-internals/#http2

firefox

about:networking

 


http2 push

 

allows the server to push other resources to a client

H2Push on

Server pushes are detected by inspecting the Link headers of responses (rel=preload)

a2enmod headers

service apache2 restart

# Server pushes are detected by inspecting the Link headers of responses
# When a link thus specified has the rel=preload attribute, it is treated as a resource to be pushed.

<Location /index.html>
    Header add Link "</css/site.css>;rel=preload"
    Header add Link "</images/logo.jpg>;rel=preload"
</Location>

.htaccess

 * Location isn't valid in .htaccess

<Files tim.htm>
    Header add Link "</test.txt>;rel=preload"
</Files>