In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to configure GZip compression when Node.js site uses Nginx as reverse proxy". In daily operation, it is believed that many people have doubts about how to configure GZip compression when Node.js site uses Nginx as reverse proxy. Xiaobian consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how to configure GZip compression when Node.js site uses Nginx as reverse proxy"! Next, please follow the editor to study!
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.
At this point, the study on "how to configure GZip compression when Node.js sites use Nginx as a reverse proxy" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.