Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nginx to take advantage of http2 with express

I am using express with node and nginx as a reverse proxy. I'd like to know how to take advantage of http/2 with nginx to serve static content, with all other requests being forwarded to the express API.

At the moment, my express server is being served via http/1 and nginx is accepting http/2 connections, and forwarding them to express. How do I set up nginx so that it uses http/2 to serve everything in my statics folder, but forwards all requests to the API as http1?

like image 756
Nodeocrat Avatar asked Oct 18 '22 22:10

Nodeocrat


1 Answers

I will break your questions into two parts:

  1. How to take advantages of http/2.0 to serve static files from nginx?
  2. How to setup nginx to send http/1.1 request to the backend server in case where nginx act as a reverse proxy?

Answer 1:

For the case of serving static files the major performance benefit can come from using the multiplexing feature of the http/2.0 protocol. Multiplexing enhances the pipelining feature introduced in http/1.1 and overcomes the problem of HOL blocking. With multiplexing you can use the same underlying TCP connection to load multiple resources in parallel using one http connection. You should also consider the stream prioritisation to assign priority to the resource which you want to load first on the page otherwise loading of some of the critical resources can be delayed since all the resources will contend for same multiplexed connection.

Answer 2:

Sending http/1.1 request to the backend server is the default behaviour. So if you have already configured nginx to use http/2.0 you do not have to do anything special to proxy http/1.1 request to your backend. This is because nginx does not support http/2.0 in proxy module as of now. Refer to this ticket. Also, please check this digital ocean tutorial which will guide you to setup nginx with http/2.0 configured on ubuntu 16.04.

like image 190
avichalp Avatar answered Oct 21 '22 01:10

avichalp