How to use Nginx as a reverse proxy to configure GZip compression


Prerequisite: node.js+nginx reverse proxy.

What node.js needs to do:

The following versions of express 4.0:

app.use(express.compress()); //Mainly this sentence app.use(express.json ()); app.use(express.urlencoded()); app.use(express.bodyparser()); app.use(express.methodoverride()); app.use(express.cookieparser());

In order to compress all requests, compress is put on it.

Express 4.0 or above (including 4.0)

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

Version 4.0 and above took out the middleware independently.

So we need your rquire('compression') first.

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

What node.js needs to do is as simple as that.

What nginx needs to do:

Open nginx configuration file, modify the configuration, and turn on 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 according to your own installation directory.

Add the following configuration on 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 (// put the node configured above)

What does each configuration item mean?

1) gzip

Syntax: gzip on/off

Default value: off

Scope: http, server, location

Description: Turn on or off the gzip module, where ON means start.

2) gzip_min_length

Syntax: gzip_min_length length

Default value: gzip_min_length 0

Scope: http, server, location

Description: Sets the minimum number of bytes allowed to be compressed, which is obtained from the content-length in the header. The default value is 0, and the page is compressed regardless of its size. It is recommended to set the number of bytes to be greater than 1k, and less than 1k may increase the pressure. |

3) gzip_buffers

Syntax: gzip_buffers number size

Default value: gzip_buffers 4 4k/8k

Scope: http, server, location

Description: Set the system to obtain several units of cache for storing the compressed data stream of gzip. 4 16k means to apply for memory in units of 16k, which is 4 times of the original data size.

4) gzip_comp_level

Syntax: gzip _ comp _ level1 ... 9

Default value: gzip_comp_level 1

Scope: http, server, location

Description: gzip compression ratio, the minimum compression ratio of 1 is the fastest, and the maximum compression ratio of 9 is the slowest (fast transmission but consuming cpu). This is set to 5.

5) gzip_types

Syntax: gzip _ types mime-type [mime-type ...]

Default: gzip_types text/html

Scope: http, server, location

Description: "text/html" type will always be compressed (whether it is specified or not) by matching the mime type for compression. Set it here as application/x-JavaScript text/CSS application/XML.

Commonly used static type are, depending on the situation that you need to compress:


Ok, the basic server has been configured here, and nginx only needs to reload.

Let's test how to use curl to test that the server has turned on gzip (the test condition is the default gzip_types, that is, only text.html is compressed, and other types are uncompressed):

To check whether gzip is enabled, the client needs to add the header information: "accept-encoding: gzip, deflate".

$ curl -i -h "accept-encoding: gzip, deflate" "http://localhost/tag.php"

http/1.1 200 okserver: nginxdate: thu, 08 mar 2012 07:23:46 gmtcontent-type: text/htmlconnection: closecontent-encoding: gzip

$ curl -i -h "accept-encoding: gzip, deflate" "http://localhost/style.css"

http/1.1 200 okserver: nginxdate: thu, 08 mar 2012 07:23:54 gmtcontent-type: text/cssconnection: closelast-modified: tue, 27 dec 2011 10:00:51 gmtetag: "bc612352322d435769c4bdc03ddb2572"content-length: 22834

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

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us