A.2. Setting up HTTP/2 via a reverse proxy server

If upgrading your main web server to support HTTP/2 isn’t possible, or if you want to test HTTP/2 without making any changes in your current setup, you may want to set up a reverse proxy in front of your web server to enable HTTP/2 support. In the following sections, I provide basic instructions for Apache and nginx.

In both these examples, the reverse proxy uses HTTP/1.1 to communicate with the backend server. Apache allows the use of HTTP/2 via the mod_proxy_http2 module. At this writing, the module is still marked as experimental,[13] and there are few benefits for proxying backend connections over HTTP/2 at this time (see the “Do you need to speak HTTP/2 all the way through?” sidebar in chapter 3), so that use case isn’t covered here. The following examples allow Apache or nginx to handle HTTP/2 communication and leave the backend server using HTTP/1.1.

13

A.2.1. Apache

For Apache to act as a proxy server, you need to enable HTTP/2 as detailed in the preceding section and to enable the following modules in the main configuration file:

proxy_module
proxy_http_module

You can enable these modules by uncommenting or adding the appropriate Load-Module lines in the main config file (httpd.conf or apache.conf) or by using a2enmod for Ubuntu-based systems.

Then you add the proxy config (assuming that the web server you want to proxy to is on localhost port 8080):

ProxyPreserveHost On

# Proxy all requests to localhost port 8080
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/

This code passes requests directly to the backend server by using the Ipv4 loopback address (127.0.0.1). You can also use the Ipv6 loopback address (::1) if you prefer. Either option is preferable to the localhost name, which requires a needless DNS lookup.

The application may need to be configured to act with a proxy server in front of it. Any links that it produces, for example, should reference the proxy port (80/443), rather than the actual port it’s running on (8080 in this example). Because reverse-proxying application servers is common, many applications make configuring this server easy with a Base URL or similar option. If the backend server doesn’t provide this option, Apache allows you to use the proxy_html_module to rewrite HTML dynamically to replace links automatically (such as replacing http://www.example.com:8080 with https://www.example.com).

A.2.2. nginx

nginx works in a similar manner to Apache, with the following config:

location / {
    proxy_pass http://127.0.0.1:8080/;
}

Similar to the comment in the Apache section, the application server may need configuring to tell it that it’s running behind a reverse proxy. If this configuration isn’t possible, the ngx_http_sub_module can dynamically rewrite any URLs similarly to proxy_html_module for Apache.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.249.104