Tutorial on configuring GZip compression when the Node.js site USES Nginx as a reverse proxy

  • 2020-05-12 06:46:41
  • OfStack

node.js developed the site if you also used nginx to implement the reverse proxy.

You can easily implement gzip compression on the server side, making your site easier to navigate.

Prerequisites: node. js + nginx reverse proxy.

node.js needs to do:

express 4.0:


app.use(express.compress()); // That's the main sentence 
app.use(express.json()); 
app.use(express.urlencoded()); 
app.use(express.bodyParser()); 
app.use(express.methodOverride()); 
app.use(express.cookieParser());

For all requests to be compressed, compress is placed on top.

express version 4.0 or above (including 4.0)


var compress = require('compression');
app.use(compress());

Version 4.0 or above brings out the middleware independently.

So first I need you to rquire('compression')

Click here to see the main differences between express 3.5 and express 4.0

node.js is as simple as that.

What nginx needs to do:

Open the nginx configuration file, modify the configuration, and turn on the gzip switch


nano /usr/local/nginx/conf/nginx.conf

nginx on your own server is not necessarily installed in the /usr/local/ directory, so look for the configuration file nginx.conf under your own installation directory

Add the following configuration to the http configuration node:


    gzip on;
    gzip_min_length 1k;
    gzip_buffers   4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types    text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

http (
    // Place the configuration node above   
)

What does each configuration item mean?

1) gzip

Grammar: gzip on/off

Default: off

Scope: http, server, location

Note: open or close the gzip module. on is used for startup

2) gzip_min_length

Syntax: gzip_min_length length

Default: gzip_min_length 0

Scope: http, server, location

Note: set the minimum number of page bytes allowed to be compressed. The page bytes are obtained from Content-Length in the header header. The default value is 0, no matter how many pages are compressed. It is recommended to set it to the number of bytes greater than 1k. If it is less than 1k, the more pressing it will be. |

3) gzip_buffers

Syntax: gzip_buffers number size

Default: gzip_buffers 4 4k/8k

Scope: http, server, location

Note: set up the system to get a few units of cache to store the gzip compressed result data stream. 4. 16k represents the application for memory in units of 16k, 4 times the original data size in units of 16k.

4) gzip_comp_level

Syntax: gzip_comp_level 1.. 9

Default: gzip_comp_level 1

Scope: http, server, location

Note: gzip compression ratio, 1 compression ratio minimum processing speed fastest, 9 compression ratio maximum but slowest processing (transmission is fast but relatively consumes cpu). I'm going to set it to 5.

5) gzip_types

Syntax: gzip_types mime-type [mime-type...]

Default: gzip_types text/html

Scope: http, server, location

Note: match the MIME type for compression, "text/html" type will always be compressed (whether or not specified). This is application/ x-javascript text/css application/xml.

Common static type has, depending on the need for compression:


text/html
text/plain
text/css
application/x-javascript
text/javascript
application/xml

OK, here the basic server has been configured, Nginx only needs reload 1.

Now let's test 1 how to use curl to test that gzip is enabled on the server (the test condition is the default gzip_types, that is, only text.html, other type are not compressed) :

To see if gzip is enabled, the client must add the "Accept-Encoding: gzip, deflate" header.


$ curl -I -H "Accept-Encoding: gzip, deflate" "http://localhost/tag.php"


HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Mar 2012 07:23:46 GMT
Content-Type: text/html
Connection: close
Content-Encoding: gzip


$ curl -I -H "Accept-Encoding: gzip, deflate" "http://localhost/style.css"


HTTP/1.1 200 OK
Server: nginx
Date: Thu, 08 Mar 2012 07:23:54 GMT
Content-Type: text/css
Connection: close
Last-Modified: Tue, 27 Dec 2011 10:00:51 GMT
ETag: "BC612352322D435769C4BDC03DDB2572"
Content-Length: 22834

You can see that. The second example is not compressed.


Related articles: