Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure GZip compression for Node.js sites

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Node.js site how to configure GZip compression related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have a harvest after reading this Node.js site how to configure GZip compression article, let's take a look at it.

Node.js development site, if you also use nginx to achieve reverse proxy.

Then in the server side can easily achieve gzip compression, so that the site browsing more smoothly.

Prerequisite: node.js + nginx reverse proxy.

What node.js needs to do:

The following versions of express:

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 version 4.0 + (including 4.0)

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

Version 4.0 or above takes out the middleware independently.

So I need you 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 the nginx configuration file, modify the configuration, turn on the gzip switch

Nano / usr/local/nginx/conf/nginx.conf

The 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 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 (/ / configure nodes above)

What does each configuration item mean?

1) gzip

Syntax: gzip on/off

Default value: off

Scope: http, server, location

Description: enable or close the gzip module. Here, use on to indicate startup.

2) gzip_min_length

Syntax: gzip_min_length length

Default value: gzip_min_length 0

Scope: http, server, location

Description: set the minimum number of bytes of pages that are allowed to be compressed. The number of page bytes is obtained from the content-length in the header header. The default value is 0, no matter how much the page is compressed. It is recommended to set the number of bytes greater than 1k. 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 up the system to get several units of cache to store the compressed result data stream of gzip. 4 16k represents 4 times the amount of memory requested in 16k units according to the original data size.

4) gzip_comp_level

Syntax: gzip_comp_level 1.. 9

Default value: gzip_comp_level 1

Scope: http, server, location

Description: gzip compression ratio, 1 compression ratio minimum processing speed is the fastest, 9 compression ratio is the largest but processing is the slowest (transmission is fast but consumes cpu). This is set to 5.

5) gzip_types

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

Default value: gzip_types text/html

Scope: http, server, location

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

Commonly used static type depends on the situation where you need to compress:

Text/htmltext/plaintext/cssapplication/x-javascripttext/javascriptapplication/xml

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

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

To check whether gzip is enabled, the client needs to add the following header: "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.

This is the end of the article on "how to configure GZip compression on a Node.js site". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to configure GZip compression on Node.js sites". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report