Real-combat Nginx (4)-Compression module and HTTP header Response message module

Source: Internet
Author: User
Tags response code

By default, Nginx gzip compression is off, gzip compression function is to allow you to save a lot of bandwidth, but will increase the cost of the server CPU, Nginx by default only text/html compression, if you want to compress the content outside the HTML, we need to manually adjust.


A. Nginx resource file Compression module introduction

Implement resource file compression module based on gzip:

The resource results sent to the client are compressed:

Ngx_http_gzip_module

The entire site content is compressed:

Ngx_http_gzip_static_module

Need to compile:--with-http_gzip_static_module

The function of this module is that after receiving the request, will go to the URL of the same path to the file system to find the extension ". gz" file, if not present, then the file gzip compression, and then sent out, so as to avoid duplication of unnecessary consumption of compressed resources, the module is not subject to gzip_types restrictions, will be valid for all requests. Therefore, it is not recommended to use the global, because most of the general is a dynamic request, there will be no. gz for this file, it is recommended only in the local directory where we confirm that there is a. gz.

We often use the resources sent to the client to compress the results, then we focus on the Ngx_http_gzip_module module;


Two. gzip Compression module detailed

1. Module installation

Gzip compression module is Ngx_http_gzip_module, is nginx built-in.


2. Module instructions

Decide whether to open the Gzip module

Gzip

Syntax: Syntax:gzip on | Off; default off: Default:gzip off; configuration segment: context:http, Server, location, if


Sets the size of the gzip request memory to request memory space in multiples of block size

Gzip_buffers

Syntax: syntax:gzip_buffers number size; default: Default:gzip_buffers 4k|16 8k; configuration segment: context:http, Server, location


Set the GZIP compression level, the lower the compression rate of the lower the file compression ratio, the slower the speed of the file compression ratio greater

Gzip_comp_level

Syntax: Syntax:gzip_comp_level level; default compression ratio is 1:default:gzip_comp_level 1; configuration segment: context:http, Server, location


When the return content is greater than this value, it is compressed using gzip, in K, with 0 o'clock, all pages are compressed

Gzip_min_length

Syntax: syntax:gzip_min_length length; default value is 20k:default:gzip_min_length 20; configuration segment: context:http, Server, location


Used to identify the version of the HTTP protocol, the earlier browser does not support gzip compression, users will see garbled, so in order to support the previous version added this option, currently this item can be ignored

Gzip_http_version

Syntax: Syntax:gzip_http_version 1.0 | 1.1; The default version is 1.1:default:gzip_http_version 1.1; configuration segment: context:http, Server, location


Nginx as a reverse proxy when enabled

Gzip_proxied

Syntax: syntax:gzip_proxied off | Expired | No-cache | No-store | Private | no_last_modified | No_etag | Auth | Any ...; Default off: default:gzip_proxied off; configuration segment: context:http, Server, location

Instruction parameters:

Off #关闭所有的代理结果数据压缩expired #启用压缩 If the header contains "Expires" header information No-cache #启用压缩 if the header contains "Cache-control:no-cache" header information No -store #启用压缩 If the header contains "Cache-control:no-store" header information private #启用压缩, if the header contains "cache-control:private" header information No_last_ Modified #启用压缩 If the header contains "last_modified" header information No_etag #启用压缩, if the header contains "ETag" header information auth #启用压缩, if the header contains "Authorizat Ion "header information any #无条件压缩所有结果数据


Set the MIME type that needs to be compressed, non-set value does not compress

Gzip_types

Syntax: Syntax:gzip_types mime-type ...; Default compressed HTML file: Default:gzip_types text/html; configuration segment: context:http, Server, location


3.nginx website Example

Gzip on;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth;gzip_types Text/plain Application/xml;


Explain:

Turn on gzip compression the minimum compressed file size is 1000kb if the header contains "Cache-control:no-cache" header information, "Cache-control:no-store" header information, Cache-control: Private "header information," Authorization "header information is enabled for compression compression of type text and XML files



Three. HTTP Header message Module

1. Module Introduction

The Ngx_http_headers_module module provides two important directives add_header and Expires to add "Expires" and "Cache-control" header fields, adding any field fields to the response header. The Add_header can be used to indicate which server the request is accessed to. The expires instruction is used to control the browser's local cache.


2. Module instructions

Add_header directive

Syntax: syntax:add_header name value [always]; Default: default:-configuration segment: context:http, Server, location, if in location


Add any domain to the response header field for response code 200,201,204,206,301,302,303,304, or 307. Such as:

Add_header from stu31.com


Expires directive

Syntax: syntax:expires [modified] Time;expires Epoch | Max | Off; default off: Default:expires off; application configuration segment: Context:http, Server, location, if in location

Add and modify actions for "Expires" and "Cache-control" in the response code of 200,201,204,206,301,302,303,304, or 307 in the header.

You can specify a positive or negative time value, and the time in the expires header is based on the current time and the time specified in the instruction.


The epoch represents the absolute time of 00:00:01 GMT from January 1, 1970, and Max specifies that the value of expires is December 31, 2037 23:59:59,cache-control The value is ten years.

The contents of the Cache-control header are specified with the preset time identifier:

• Set the time value to a negative number: Cache-control:no-cache.

• Set to a positive or 0 time value: cache-control:max-age = #, where # is the unit of seconds, specified in the instruction.

The parameter off disables modifying "Expires" and "Cache-control" in the answer header.


Example: On a picture, flash files are cached locally in the browser for 30 days

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $ {expires 30d;}



Four. Enable gzip compression and custom HTTP header messages for virtual hosts


1. Modify the virtual host configuration file, add gzip compression and HTTP header message

[[email protected] ~]# vim /etc/nginx/extra/nginx-vhost.conf server {         gzip  on;         gzip_http_version 1.1;        gzip_vary on;         gzip_proxied expired no-cache no-store private  auth;        gzip_comp_level 7;         gzip_buffers 16 8k;        gzip_types  text/plain text/html application/xml application/json;         gzip_min_length 1024;        listen    *:80 default_server;        server_name www.stu31.com;       &Nbsp; index index.html index.htm ;        root   /www/vhosts/www1;        add_header X-header  testheader2014;        access_log  /var/log/nginx/ www.stu31.com.log main ;        location /status {                 stub_status  on;                auth_ basic  "Nginx-status";                 auth_basic_user_file /etc/nginx/.htpasswd;                 allow 172.16.0.0/16;                 deny all;        }} 


2. Grammar check:

[[Email protected] ~]# service nginx Configtestnginx: [Warn] duplicate MIME type "text/html" In/etc/nginx/extra/nginx-vho St.conf:8nginx:the configuration file/etc/nginx/nginx.conf syntax is oknginx:configuration file/etc/nginx/nginx.conf Test is successful

Reason: text/html don't need to write at all, Gzip will compress it by default, but the previous Nginx version does not prompt this warning, the new version will be out of this warning.



3. Restart the Nginx service:

[[email protected] ~]# service nginx restart                  nginx: the configuration file  /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/ nginx.conf test is successfulstopping nginx:                                               [  OK  ]Starting nginx:                                              [   OK  ]

4. Connection test:

You can see a custom in the header of the response message:

[Email protected] ~]# curl-i http://172.16.31.40HTTP/1.1 okserver:nginx/1.6.2date:sat, Dec 05:56:57 Gmtcon Tent-type:text/htmlcontent-length:27last-modified:sat, 02:09:31 Gmtconnection:keep-aliveetag: " 549E14DB-1B "X-header:testheader2014accept-ranges:bytes


Compression is also successful:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/93/wKioL1SevlLzbybtAAOyoKeRcjo591.jpg "title=" 7gzip.jpg "alt=" Wkiol1sevllzbybtaaoyokercjo591.jpg "/>

At this point, nginx resource Compression module and the HTTP header message module is complete.


This article is from "Dragon Guardian" blog, please make sure to keep this source http://sohudrgon.blog.51cto.com/3088108/1596696

Real-combat Nginx (4)-Compression module and HTTP header Response message module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.