This article has been written in accompany with another article, Best Practice for HTTP2 Front-End Deployments — Part Two. It will make use of Homebrew on Mac OS X in order to install NGINX with HTTP/2 support. So, fire up a terminal and let's get started.

1. Install Homebrew

If you don't already have Homebrew, then you should install it first. Copy and paste the command below:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

2. Install NGINX

First, let's update the list of homebrew packages:

brew update

Now, let's compile and install NGINX with http2:

brew install --with-http2 nginx

Next, let's double check we have the right version of NGINX. Also, it's worth noting that HTTP/2 will only work with version 1.9.5 and above.

nginx -v

You should see nginx version: nginx/1.10.1 at the time of writing. Please make sure it's above 1.9.5.

Now, let's check that NGINX is working. Make sure that port 8080 is available. We're using Sudo, so you may be prompted for your password:

sudo nginx
open http://localhost:8080/

You should now see the Welcome to NGINX! page. For the moment, we can stop NGINX by using:

sudo nginx -s stop

3. Configure NGINX to Use SSL and HTTP/2

Next, we need to make some changes to nginx.conf. Use your favorite text editor to open nginx.conf. In the example below, we're using Texmate.

mate /usr/local/etc/nginx/nginx.conf

The changes that we need to make are shown below in the server {} block:

server {  
    listen                     443 ssl http2;
    server_name                localhost;

    ssl                        on;
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate            nginx.crt;
    ssl_certificate_key        nginx.key;

    location / {
        proxy_pass          http://localhost:8080;
        proxy_set_header    Host      $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-HTTPS   'True';
    }
}

4. Generate an SSL Certificate

HTTP/2 requires a secure connection over TLS (the new name for SSL). Before we can use https, we first need to generate an SSL certificate:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /usr/local/etc/nginx/nginx.key -out /usr/local/etc/nginx/nginx.crt

This is a self-signed certificate, meaning that you'll see a browser security warning when using it. You can add a security exception for your local development domain.

5. Restart NGINX

sudo nginx

Now you can (or should be able to) hit https://localhost/, and NGINX should proxy pass to your local development server.

To check that HTTP/2 is working, you can open the web inspector in your browser and check the network page. In the protocol column, you should now see h2 or http2 next to each request.