gzip compression
use gzip compression to reduce web site bandwidth consumption while increasing access speed.
Mainly in the Nginx server to compress the page, and then in the browser side of the decompression and resolution,
Most popular browsers are currently sluggish in gzip-formatted compression, so don't worry.
By default, Nginx gzip compression is turned off, and Nginx only compresses text/html by default
The main configuration is as follows:
gzip on; #开启
gzip_http_version 1.0; #默认1.1
gzip_vary on;
Gzip_comp_level 6;
Gzip_proxied any;
Gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml Application/xml+rss text/javascript #压缩的文件类型
gzip_buffers 8k; #设置gzip申请内存的大小 to request the memory space by a multiple of the block Size setting gzip request memory Size , the effect is to request memory space by a multiple of the block size
# Disable gzip for certain browsers.
Gzip_disable "MSIE [1-6]. (?!. *SV1) "#ie6不支持gzip, need to disable IE6, damn it!!!!
Note: The Gzip_http_version setting in which the default value is 1.1, which means that the request for the http/1.1 protocol will be gzip compressed
If we use Proxy_pass for reverse proxy, then the http/1.0 protocol is used to communicate between the Nginx and the backend upstream server.
Gzip parameter Description:
decide whether to open the Gzip module
Param:on|off
Example:gzip on;
Gzip_buffers
Set the size of the gzip request memory to request memory space by a multiple of the block size
Param1:int Increase in multiples
Param2:int (k) the rear unit is K
Example:gzip_buffers 4 8k;
Gzip_comp_level
Set the GZIP compression level, the lower the level of compression faster file compression than the smaller, whereas slower file compression than the larger
Param:1-9
Example:gzip_com_level 1;
Gzip_min_length
When the return content is greater than this value, gzip is used for compression, in K, at 0 o'clock, all pages are compressed
Param:int
Example:gzip_min_length 1000;
Gzip_http_version
Used to identify the version of the HTTP protocol, the early browsers do not support gzip compression, users will see garbled, so in order to support the previous version of this option, the current basic can be ignored
param:1.0|1.1
Example:gzip_http_version 1.0
Gzip_proxied
Nginx is enabled as a reverse proxy,
Param:off|expired|no-cache|no-sotre|private|no_last_modified|no_etag|auth|any]
Expample:gzip_proxied No-cache;
off– shutdown all proxy results data compression
expired– enable compression If the header contains "Expires" header information
no-cache– enable compression If the header contains "Cache-control:no-cache" header information
no-store– enable compression If the header contains "Cache-control:no-store" header information
private– enable compression If the header contains "Cache-control:private" header information
no_last_modified– enable compression If the header contains "last_modified" header information
no_etag– enable compression If the header contains "ETag" header information
auth– enable compression If the header contains "Authorization" header information
any– unconditionally compresses all result data
Gzip_types
Sets the MIME type that needs to be compressed, and the non-set value is not compressed
Param:text/html|application/x-javascript|text/css|application/xml
Example:gzip_types text/html;
Gzip_vary on;
and HTTP headers have a relationship, add a vary head, to the proxy server, and some browsers support compression, some do not support, so avoid wasting unsupported also compression, so according to the client's HTTP headers to determine whether the need to compress
Nginx and GZIP Requests
the Nginx gzip modules for generic threads are all related to response gzip, but what if you need gzip related to request? Look at the following:
Scheme
The first option is to use Lua-zlib:
Local zlib = require "zlib" local
encoding = Ngx.req.get_headers () ["content-encoding"]
if encoding = "gzip" Then Local Body
= Ngx.req.get_body_data () If the body then the local
stream = zlib.inflate ()
Ngx.req.set_body_data (Stream (body))
End End
The second option is through the Luajit FFI library to wrap the Zlib module, the official tutorial has some ready-made examples, but the example is deflate, rather than gzip, with the FFI package gzip is a little complicated, fortunately, others have done the relevant work, That's lua-files:
Local FFI = Require ' ffi ' local
zlib = Require ' zlib ' local
function reader (s) The local do return
function ( If done then return end do
= True return
S-end local
function writer () local
t = {}< C12/>return function (data, SZ)
if not data then return Table.concat (t) end
t[#t + 1] = ffi.string (data, SZ)
end End local
encoding = ngx.req.get_headers () ["content-encoding"]
if encoding = "gzip" then
Local BODY = Ngx.req.get_body_data ()
if the body then
the local write = writer ()
zlib.inflate (the body) Write, nil, "gzip")
Ngx.req.set_body_data (write ())
end
As the example code from Zlib_test.lua, at first glance, the code reader and writer may be confusing, in fact, you can interpret them as input and output interface, can be modified into files, databases and so on.
Don't be too early, when you run, you are likely to encounter the following error:
Libzlib.so:cannot open Shared object file.
This is actually because of the following Zlib.lua code:
Local C = Ffi.load ' zlib '
Run time, Ffi.load will automatically fill the file name, if it is windows, load the Zlib.dll file, if it is Linux, then load the libzlib.so, but in fact under Linux, Zlib extension is the name libz.so, not libzlib.so.
Knowing the story of the problem, we naturally know how to modify the code:
Local C
if Ffi.os = = "Windows" then
C = ffi.load "zlib"
else
c = ffi.load "Z"
end
Sometimes we do not recommend directly modify the Third-party library code, because in this case, each third library to update the code, we have to do the corresponding changes, once forgotten will be wrong, at this time to consider doing a soft connection alias.
Test
The opening says that interfaces are all done in PHP, but the gzip data in the request is handled in Lua, how do you get PHP to use the data that LUA processes? Different languages seem to be a problem, fortunately Nginx has phases, PHP as a fastcgi module working in the content phase, LUA can work in the access phase, so that they are harmonious:
Location ~ \.php$ {
access_by_lua_file/path/to/lua/file;
Include fastcgi.conf;
Fastcgi_pass 127.0.0.1:9000;
}
So what are the efficiencies of the two lua-zlib and lua-files programs? Here is the test script I wrote in PHP:
<?php
$url = ' http://url ';
$header = Implode ("\ r \ n", Array (
' content-type:application/x-www-form-urlencoded ',
' content-encoding: Gzip ',
' connection:close ',
));
$content = Gzencode (Http_build_query ('
foo ' => str_repeat (' x ', MB),
' Bar ' => str_repeat (' y ', 100) ,
)));
$options = Array ('
http ' => array ('
protocol_version ' => ' 1.1 ', ' method
' => ' POST '),
' Header ' => $header,
' content ' => $content,)
;
$context = Stream_context_create ($options);
for ($i = 0; $i < 1000 $i + +) {
file_get_contents ($url, False, $context);
>
A lot of people write test scripts, like at the beginning of the end of the time to add, so that the subtraction of the actual operation of the code time, in fact, this is not necessary, the use of Linux from the time you can get the runtime:
Shell> Time Php/path/to/php/file