]> granicus.if.org Git - apache/blob - docs/manual/howto/http2.xml
a27864b5ec006a66d1baf49ef1b0b8628aab7499
[apache] / docs / manual / howto / http2.xml
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
5
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
13
14      http://www.apache.org/licenses/LICENSE-2.0
15
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
22
23 <manualpage metafile="http2.xml.meta">
24 <parentdocument href="./">How-To / Tutorials</parentdocument>
25
26   <title>HTTP/2 guide</title>
27
28   <summary>
29     <p>This is the howto guide for the HTTP/2 implementation in Apache httpd. This
30     feature is <em>experimental</em> and you may expect interfaces and directives to
31     change between releases.
32     </p>
33   </summary>
34   <seealso><a href="../mod/mod_http2.html">mod_http2</a></seealso>
35
36   <section id="protocol">
37     <title>The HTTP/2 protocol</title>
38     <p>HTTP/2 is the evolution of the world's most successful application layer protocol, HTTP.
39     It focuses on making more efficient use of network resources. It does not change the fundamentals
40     of HTTP, the semantics. There are still request and responses and headers and all that. So, if
41     you already know HTTP/1, you know 95% about HTTP/2 as well.</p>
42     <p>There has been a lot written about HTTP/2 and how it works. The most normative is, of course,
43     its <a href="https://tools.ietf.org/html/rfc7540">RFC 7540</a> 
44     (<a href="http://httpwg.org/specs/rfc7540.html">also available in more readable formatting, YMMV</a>).
45     So, there you'll find the nuts and bolts.</p>
46     <p>But, as RFC do, it's not really a good thing to read first. It's better to first understand
47     <em>what</em> a thing wants to do and then read the RFC about <em>how</em> it is done. A much
48     better document to start with is <a href="https://daniel.haxx.se/http2/">http2  explained</a>
49     by Daniel Stenberg, the author of <a href="https://curl.haxx.se">curl</a>. It is available in
50     an ever growing list of languages, too!</p>
51     <p>Too Long, Didn't read: there are some new terms and gotchas that need to be kept in mind while reading this document:</p>
52     <ul>
53         <li>HTTP/2 is a <strong>binary protocol</strong>, as opposed to HTTP 1.1 that is plain text. The latter is meant to be human readable (for example sniffing network traffic) meanwhile the former is not. More info in the official FAQ <a href="https://http2.github.io/faq/#why-is-http2-binary">question</a>.</li>
54         <li><strong>h2</strong> is HTTP/2 over TLS (protocol negotiation via ALPN).</li>
55         <li><strong>h2c</strong> is HTTP/2 over TCP.</li>
56         <li>A <strong>frame</strong> is the smallest unit of communication within an HTTP/2 connection, consisting of a header and a variable-length sequence of octets structured according to the frame type. More info in the official documentation <a href="http://httpwg.org/specs/rfc7540.html#FramingLayer"> section</a>.</li>
57         <li>A <strong>stream</strong> is a bidirectional flow of frames within the HTTP/2 connection. The correspondent concept in HTTP 1.1 is a request/response message exchange. More info in the official documentation <a href="http://httpwg.org/specs/rfc7540.html#StreamsLayer"> section</a>.</li>
58         <li>HTTP/2 is able to run <strong>multiple streams</strong> of data over the same TCP connection, avoiding the classic HTTP 1.1 head of blocking slow request and avoiding to re-instantiate TCP connections for each request/response (KeepAlive patched the problem in HTTP 1.1 but did not fully solve it).</li>
59     </ul>
60   </section>
61
62   <section id="implementation">
63     <title>HTTP/2 in Apache httpd</title>
64     <p>The HTTP/2 protocol is implemented by its own httpd module, aptly named
65     <a href="../mod/mod_http2.html">mod_http2</a>. It implements the complete set
66     of features described by RFC 7540 and supports HTTP/2 over cleartext (http:), as
67     well as secure (https:) connections. The cleartext variant is named '<code>h2c</code>', 
68     the secure one '<code>h2</code>'. For <code>h2c</code> it allows the <em>direct</em>
69     mode and the <code>Upgrade:</code> via an initial HTTP/1 request.</p>
70     <p>One feature of HTTP/2 that offers new capabilities for web developers is
71     <a href="#push">Server Push</a>. See that section on how your web application
72     can make use of it.</p>
73   </section>
74   
75   <section id="building">
76     <title>Build httpd with HTTP/2 support</title>
77     <p><a href="../mod/mod_http2.html">mod_http2</a> uses the library of <a href="https://nghttp2.org">nghttp2</a>
78     as its implementation base. In order to build <code>mod_http2</code> you need at least version 1.2.1 of
79     <code>libnghttp2</code> installed on your system.</p>
80     <p>When you <code>./configure</code> you Apache httpd source tree, you need to give it 
81     '<code>--enable-http2</code>' as additional argument to trigger the build of the module.
82     Should your <code>libnghttp2</code> reside in an unusual place (whatever that is on your
83     operating system), you may announce its location with '<code>--with-nghttp2=&lt;path&gt;</code>'
84     to <code>configure</code>.</p>
85     <p>While that should do the trick for most, they are people who might prefer a statically
86     linked <code>nghttp2</code> in this module. For those, the option <code>--enable-nghttp2-staticlib-deps</code>
87     exists. It works quite similar to how one statically links openssl to mod_ssl.</p>
88     <p>Speaking of SSL, you need to be aware that most browsers will speak HTTP/2 only on <code>https:</code>
89     URLs, so you need a server with SSL support. But not only that, you will need a SSL library
90     that supports the <code>ALPN</code> extension. If OpenSSL is the library you use, you need
91     at least version 1.0.2.</p>
92   </section>
93
94   <section id="basic-config">
95     <title>Basic Configuration</title>
96
97     <p>When you have a <code>httpd</code> built with <code>mod_http2</code> you need some
98     basic configuration for it becoming active. The first thing, as with every Apache module,
99     is that you need to load it:</p>
100     <highlight language="config">
101 LoadModule http2_module modules/mod_http2.so
102     </highlight>
103     
104     <p>The second directive you need to add to your server configuration is</p>
105     <highlight language="config">
106 Protocols h2 http/1.1
107 </highlight>
108     <p>This allows h2, the secure variant, to be the preferred protocol on your server
109     connections. When you want to enable all HTTP/2 variants, you simply write:</p>
110     <highlight language="config">
111 Protocols h2 h2c http/1.1
112 </highlight>
113     <p>Depending on where you put this directive, it affects all connections or just
114     the ones to a certain virtual host. You can nest it, as in:</p>
115     <highlight language="config">
116 Protocols http/1.1
117 &lt;VirtualHost ...&gt;
118     ServerName test.example.org
119     Protocols h2 http/1.1
120 &lt;/VirtualHost&gt;
121 </highlight>
122
123     <p>This allows only HTTP/1 on connections, except SSL connections to <code>test.example.org</code>
124     which offer HTTP/2.</p>
125     <note><title>Choose a strong SSLCipherSuite</title>
126      <p>The <directive module="mod_ssl">SSLCipherSuite</directive> needs to be configured with a strong TLS cipher suite. The current version of mod_http2 does not enforce any cipher but most clients do so. Pointing a browser to a <code>h2</code> enabled server with a inappropriate cipher suite will force it to simply refuse and fall back to HTTP 1.1. This is a common mistake that is done while configuring httpd for HTTP/2 the first time, so please keep it in mind to avoid long debugging sessions! If you want to be sure about the cipher suite to choose please avoid the ones listed in the <a href="http://httpwg.org/specs/rfc7540.html#BadCipherSuites">HTTP/2 TLS blacklist</a>.</p>
127     </note>
128     <p>The order of protocols mentioned is also relevant. By default, the first one is the 
129     most preferred protocol. When a client offers multiple choices, the one most to the 
130     left is selected. In</p>
131     <highlight language="config">
132 Protocols http/1.1 h2
133 </highlight>
134     <p>the most preferred protocol is HTTP/1 and it will always be selected unless a 
135     client <em>only</em> supports h2. Since we want to talk HTTP/2 to clients that
136     support it, the better order is</p>
137     <highlight language="config">
138 Protocols h2 h2c http/1.1
139 </highlight>
140
141     <p>There is one more thing to ordering: the client has its own preferences, too. If
142     you want, you can configure your server to select the protocol most preferred by
143     the client:</p>
144     <highlight language="config">
145 ProtocolsHonorOrder Off
146     </highlight>
147     <p>makes the order <em>you</em> wrote the Protocols irrelevant and only the client's
148     ordering will decide.</p>
149     <p>A last thing: the protocols you configure are not checked for correctness
150     or spelling. You can mention protocols that do not exist, so there is no need
151     to guard <code>Protocols</code> with any <code>IfModule</code> checks.</p>
152     <p>For more advanced tips on configuration, see the <a href="../mod/mod_http2.html#dimensioning">
153     modules section about dimensioning</a> and <a href="../mod/mod_http2.html#misdirected">
154     how to manage multiple hosts with the same certificate</a>.</p>
155   </section>
156
157   <section id="clients">
158     <title>Clients</title>
159     <p>Almost all modern browsers support HTTP/2, but only over SSL connections: Firefox (v43),
160     Chrome (v45), Safari (since v9), iOS Safari (v9), Opera (v35), Chrome for Android (v49)
161     and Internet Explorer (v11 on Windows10) (<a href="http://caniuse.com/#search=http2">source</a>).</p>
162     <p>Other clients, as well as servers, are listed 
163     <a href="https://github.com/http2/http2-spec/wiki/Implementations">on the Implementations wiki</a>,
164     among them implementations for c, c++, common lisp, dart, erlang, haskell, java, nodejs,  php, 
165     python, perl, ruby, rust, scala and swift.</p>
166     <p>Several of the non-browser client implementations support HTTP/2 over cleartext, h2c. The
167     most versatile being <a href="https://curl.haxx.se">curl</a>.</p>
168   </section>
169
170   <section id="tools">
171     <title>Useful tools to debug HTTP/2</title>
172     <p>The first tool to mention is of course <a href="https://curl.haxx.se">curl</a>. Please make sure that
173     your version supports HTTP/2 checking its <code>Features</code>:</p>
174     <highlight language="config">
175     $ curl -V
176     curl 7.45.0 (x86_64-apple-darwin15.0.0) libcurl/7.45.0 OpenSSL/1.0.2d zlib/1.2.8 nghttp2/1.3.4
177     Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 [...] 
178     Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP <strong>HTTP2</strong>
179     </highlight>
180     <note><title>Mac OS homebrew notes</title>
181     brew install curl --with-openssl --with-nghttp2 
182     </note>
183     <p>And for really deep inspection <a href="https://wiki.wireshark.org/HTTP2">wireshark</a>.</p>
184     <p>The <a href="https://nghttp2.org">nghttp2</a> package also includes clients, such as:</p>
185     <ul>
186         <li><a href="https://nghttp2.org/documentation/nghttp.1.html">nghttp</a> - useful to visualize the HTTP/2 frames and get a better idea of the protocol.</li>
187         <li><a href="https://nghttp2.org/documentation/h2load-howto.html">h2load</a> - useful to stress-test your server.</li>
188     </ul>
189     <p>Chrome offers detailed HTTP/2 logs on its connections via the 
190     <a href="chrome://net-internals/#http2">special net-internals page</a>. There is also an interesting extension for <a href="https://chrome.google.com/webstore/detail/http2-and-spdy-indicator/mpbpobfflnpcgagjijhmgnchggcjblin?hl=en">Chrome</a> and <a href="https://addons.mozilla.org/en-us/firefox/addon/spdy-indicator/">Firefox</a> to visualize when your browser is using HTTP/2.</p>
191   </section>
192
193   <section id="push">
194     <title>Server Push</title>
195     <p>The HTTP/2 protocol allows the server to PUSH responses to a client it never
196     asked for. The tone of the conversation is: &quot;here is a request that you
197     never sent and the response to it will arrive soon...&quot;</p>
198     <p>But there are restrictions: the client can disable this feature and the
199     server may only ever PUSH on a request that came from the client.</p>
200     <p>The intention is to allow the server to send resources to the client that
201     it will most likely need: a css or javascript resource that belongs to a html
202     page the client requested. A set of images that is referenced by a css, etc.</p>
203     <p>The advantage for the client is that it saves the time to send the request which
204     may range from a few milliseconds to half a second, depending on where on the 
205     globe both are located. The disadvantage is that the client may get sent
206     things it already has in its cache. Sure, HTTP/2 allows for the early cancellation
207     of such requests, but still there are resources wasted.</p>
208     <p>To summarize: there is no one good strategy on how to make best use of this 
209     feature of HTTP/2 and everyone is still experimenting. So, how do you experiment
210     with it in Apache httpd?</p>
211     <p><code>mod_http2</code> inspect response header for <code>Link</code> headers
212     in a certain format:</p>
213     <highlight language="config">
214 Link &lt;/xxx.css&gt;;rel=preload, &lt;/xxx.js&gt;; rel=preload
215     </highlight>
216     <p>If the connection supports PUSH, these two resources will be sent to the
217     client. As a web developer, you may set these headers either directly in
218     your application response or you configure the server via</p>
219     <highlight language="config">
220 &lt;Location /xxx.html&gt;
221     Header add Link "&lt;/xxx.css&gt;;rel=preload"
222     Header add Link "&lt;/xxx.js&gt;;rel=preload"
223 &lt;/Location&gt;
224     </highlight>
225     <p>If you want to use <code>preload</code> links without triggering a PUSH, you
226     can use the <code>nopush</code> parameter, as in</p>
227     <highlight language="config">
228 Link &lt;/xxx.css&gt;;rel=preload;nopush
229     </highlight>
230     <p>or you may disable PUSHes for your server entirely with the directive</p>
231     <highlight language="config">
232 H2Push Off
233     </highlight>
234     <p>And there is more:</p>
235     <p>The module will keep a diary of what has been PUSHed for each connection
236     (hashes of URLs, basically) and will not PUSH the same resource twice. When
237     the connection closes, this information is discarded.</p>
238     <p>There are people thinking about how a client can tell a server what it
239     already has, so PUSHes for those things can be avoided, but this is all
240     highly experimental right now.</p>
241     <p>Another experimental draft that has been implemented in <code>mod_http2</code>
242     is the <a href="https://tools.ietf.org/html/draft-ruellan-http-accept-push-policy-00">
243     Accept-Push-Policy Header Field</a> where a client can, for each request, define
244     what kind of PUSHes it accepts.</p>
245   </section>
246   
247 </manualpage>