The following set of modules provides functionalities having an effect on the contents served to the client, either by modifying the way the response is encoded, by affecting the headers, or by generating a response from scratch.

Empty GIF

The purpose of this module is to provide a directive that serves a 1 x 1 transparent GIF image from the memory. Such files are sometimes used by web designers to tweak the appearance of their website. With this directive, you get an empty GIF straight from the memory instead of reading and processing an actual GIF file from the storage space.

To utilize this feature, simply insert the empty_gif directive in the location of your choice:

location = /empty.gif {
  empty_gif;
}

FLV and MP4

FLV and MP4 are separate modules enabling a simple functionality that becomes useful when serving Flash (FLV) or MP4 video files. It parses a special argument of the request, start, which indicates the offset of the section the client wishes to download or pseudo-stream. The video file must thus be accessed with the following URI: video.flv?start=XXX. This parameter is prepared automatically by mainstream video players such as JWPlayer.

This module is not included in the default Nginx build.

To utilize this feature, simply insert the flv or mp4 directive in the location of your choice:

location ~* \.flv {
  flv;
}
location ~* \.mp4 {
  mp4;
}

Be aware that in case Nginx fails to seek to the requested position within the video file, the request will result in a 500 Internal Server Error HTTP response. JWPlayer sometimes misinterprets this error and simply displays a "Video not found" error message.

HTTP Headers

Two directives are introduced by this module that will affect the header of the response sent to the client.

First, add_header Name value lets you add a new line in the response headers, respecting the following syntax: Name: value. , 201, 204, 301, 302, and 304. You may insert variables in the value argument.

Additionally, the expires directive allows you to control the value of the Expires and Cache-Control HTTP header sent to the client, affecting requests of the same code, as listed above. It accepts a single value among the following:

  • off: Does not modify either headers.
  • A time value: The expiration date of the file is set to the current time +, the time you specify. For example, expires 24h will return an expiry date set to 24 hours from now.
  • epoch: The expiration date of the file is set to January 1, 1970. The Cache-Control header is set to no-cache.
  • max: The expiration date of the file is set to December 31, 2037. The Cache-Control header is set to 10 years.

Addition

The Addition module allows you (through simple directives) to add content before or after the body of the HTTP response.

The two main directives are:

add_before_body file_uri;
add_after_body file_uri;

As stated previously, Nginx triggers a sub-request for fetching the specified URI. Additionally, you can define the type of files to which the content is appended in case your location block pattern is not specific enough (default: text/html):

addition_types mime_type1 [mime_type2…];
addition_types *;

Substitution

Along the lines of the previous module, the Substitution module allows you to search and replace text directly from the response body:

sub_filter searched_text replacement_text;

This module is not included in the default Nginx build.

Two additional directives provide more flexibility:

  • sub_filter_once (on or off, default on): Only replaces the text once and stops after the first occurrence.
  • sub_filter_types (default text/html): Affects additional MIME types that will be eligible for the text replacement. The * wildcard is allowed.

Gzip Filter

This module allows you to compress the response body with the Gzip algorithm before sending it to the client. To enable Gzip compression, use the gzip directive (on or off) at the http, server, location, and even the if level (though that is not recommended). The following directives will help you further configure the filter options:


gzip_buffers

Context: http, server, location

Defines the amount and size of buffers to be used for storing the compressed response.

Syntax: gzip_buffers amount size;

Default: gzip_buffers 4 4k (or 8 k depending on the OS).


gzip_comp_level

Context: http, server, location

Defines the compression level of the algorithm. The specified value ranges from 1 (low compression, faster for the CPU) to 9 (high compression, slower).

Syntax: Numeric value.

Default: 1


gzip_disable

Context: http, server, location

Disables Gzip compression for requests where the User-Agent HTTP header matches the specified regular expression.

Syntax: Regular expression

Default: None


gzip_http_version

Context: http, server, location

Enables Gzip compression for the specified protocol version.

Syntax: 1.0 or 1.1

Default: 1.1


gzip_min_length

Context: http, server, location

If the response body length is inferior to the specified value, it is not compressed.

Syntax: Numeric value (size)

Default: 0


gzip_proxied

Context: http, server, location

Enables or disables Gzip compression for the body of responses received from a proxy.

The directive accepts the following parameters; some can be combined:

  • off/any: Disables or enables compression for all requests
  • expired: Enables compression if the Expires header prevents caching
  • no-cache/no-store/private: Enables compression if the Cache-Control header is set to no-cache, no-store, or private
  • no_last_modified: Enables compression in case the LastModified header is not set
  • no_etag: Enables compression in case the ETag header is not set
  • auth: Enables compression in case an Authorization header is set

gzip_types

Context: http, server, location

Enables compression for types other than the default text/html MIME type.

Syntax:

gzip_types mime_type1 [mime_type2…];
gzip_types *;

Default: text/html (cannot be disabled)


gzip_vary

Context: http, server, location

Adds the Vary: Accept-Encoding HTTP header to the response.

Syntax: on or off

Default: off


gzip_window

Context: http, server, location

Sets the size of the window buffer (windowBits argument) for Gzipping operations. This directive value is used for calls to
functions from the Zlib library.

Syntax: Numeric value (size)

Default: MAX_WBITS constant from the Zlib library


gzip_hash

Context: http, server, location

Sets the amount of memory that should be allocated for the internal compression state (memLevel argument). This directive
value is used for calls to functions from the Zlib library.

Syntax: Numeric value (size)

Default: MAX_MEM_LEVEL constant from the Zlib prerequisite library


postpone_gzipping

Context: http, server, location

Defines a minimum data threshold to be reached before starting the Gzip compression.

Syntax: Size (numeric value)

Default: 0


gzip_no_buffer

Context: http, server, location

By default, Nginx waits until at least one buffer (defined by gzip_buffers) is filled with data before sending the response to the client. Enabling this directive disables buffering.

Syntax: on or off

Default: off


Gzip static

This module adds a simple functionality to the Gzip filter mechanism — when its gzip_static directive (on or off) is enabled, Nginx will automatically look for a .gz file corresponding to the requested document before serving it. This allows Nginx to send pre-compressed documents instead of compressing documents on-the-fly at each request.

This module is not included in the default Nginx build.

If a client requests /documents/page.html, Nginx checks for the existence of a /documents/page.html.gz file. If the .gz file is found, it is served to the client. Note that Nginx does not generate .gz files itself, even after serving the requested files.

Charset Filter

With the Charset Filter module, you can control the character set of the response body more accurately. Not only are you able to specify the value of the charset argument of the Content-Type HTTP header (such as Content-Type: text/html; charset=utf-8), but Nginx can also re-encode data to a specified encoding method automatically.


charset

Context: http, server, location, if

This directive adds the specified encoding to the Content-Type header of the response. If the specified encoding differs from the source_charset one, Nginx re-encodes the document.

Syntax: charset encoding | off;

Default: off

Example: charset utf-8;


source_charset

Context: http, server, location, if

Defines the initial encoding of the response; if the value specified in the charset directive differs, Nginx re-encodes the document.

Syntax: source_charset encoding;


override_charset

Context: http, server, location, if

When Nginx receives a response from the proxy or FastCGI gateway, this directive defines whether or not the character encoding should be checked and potentially overridden.

Syntax: on or off

Default: off


charset_types

Context: http, server, location

Defines the MIME types that are eligible for re-encoding.

Syntax:
charset_types mime_type1 [mime_type2…];
charset_types * ;

Default: text/html, text/xml, text/plain, text/vnd.wap.wml, application/x-javascript, application/rss+xml


charset_map

Context: http

Lets you define character re-encoding tables. Each line of the table contains two hexadecimal codes to be exchanged. You will find reencoding tables for the koi8-r character set in the default Nginx configuration folder (koi-win and koi-utf).

Syntax: charset_map src_encoding dest_encoding { … }


Memcached

Memcached is a daemon application that can be connected to via sockets. Its main purpose, as the name suggests, is to provide an efficient distributed key/value memory caching system. The Nginx Memcached module provides directives allowing you to configure access to the Memcached daemon.


memcached_pass

Context: location, if

Defines the hostname and port of the Memcached daemon.

Syntax: memcached_pass hostname:port;

Example: memcached_pass localhost:11211;


memcached_bind

Context: http, server, location

Forces Nginx to use the specified local IP address for connecting to the Memcached server. This can come in handy if your server has multiple network cards connected to different networks.

Syntax: memcached_bind IP_address;

Example: memcached_bind 192.168.1.2;


memcached_connect_timeout

Context: http, server, location

Defines the connection timeout in milliseconds (default: 60,000).

Example: memcached_connect_timeout 5000;


memcached_send_timeout

Context: http, server, location

Defines the data writing operations timeout in milliseconds (default: 60,000).

Example: memcached_send_timeout 5,000;


memcached_read_timeout

Context: http, server, location

Defines the data reading operations timeout in milliseconds (default: 60,000).

Example: memcached_read_timeout 5,000;


memcached_buffer_size

Context: http, server, location

Defines the size of the read and write buffer, in bytes (default: page size).

Example: memcached_buffer_size 8k;


memcached_next_upstream

Context: http, server, location

When the memcached_pass directive is connected to an upstream block, this directive defines the conditions that should be matched in order to skip to the next upstream server.

Syntax: Values selected among error timeout, invalid_response, not_found, or off

Default: error timeout

Example: memcached_next_upstream off;


Additionally, you will need to define the $memcached_key variable that defines the key of the element that you are placing or fetching from the cache. You may, for instance, use set $memcached_key $uri or set $memcached_key $uri?$args.

Note that the Nginx Memcached module is only able to retrieve data from the cache; it does not store the result of requests. Storing data in the cache should be done by a server-side script. You just need to make sure to employ the same key naming scheme in both your server-side scripts and the Nginx configuration. As an example, we could decide to use memcached to retrieve data from the cache before passing the request to a proxy, if the requested URI is not found:

server {
  server_name example.com;
  […]
  location / {
    set $memcached_key $uri;
    memcached_pass 127.0.0.1:11211;
    error_page 404 @notcached;
  }
  location @notcached {
    internal;
    # if the file is not found, forward request to proxy
    proxy_pass 127.0.0.1:8080;
  }
}

Image Filter

This module provides image processing functionalities through the GD Graphics Library (also known as gdlib).

This module is not included in the default Nginx build.

Make sure to employ the following directives on a location block that filters image files only, such as location ~* \.(png|jpg|gif)$ { … }.


image_filter

Context: location

Lets you apply a transformation on the image before sending it to the client. There are five options available:

  • test: Makes sure that the requested document is an image file, returns a 415 Unsupported Media Type HTTP error if the test fails.
  • size: Composes a simple JSON response indicating information about the image such as the size and type (for example; { "img": { "width":50, "height":50, "type":"png"}}). If the file is invalid, a simple {} is returned.
  • resize width height: Resizes the image to the specified dimensions.
  • crop width height: Selects a portion of the image of the specified dimensions.
  • rotate 90 | 180 | 270: Rotates the image by the specified angle (in degrees).

Example: image_filter resize 200 100;


image_filter_buffer

Context: http, server, location

Defines the maximum file size for images to be processed.

Default: image_filter_buffer 1m;


image_filter_jpeg_quality

Context: http, server, location

Defines the quality of output JPEG images.

Default: image_filter_jpeg_quality 75;


image_filter_transparency

Context: http, server, location

By default, PNG and GIF images keep their existing transparency during operations you perform using the Image Filter module. If you set this directive to off, all existing transparency will be lost but the image quality will be improved.

Syntax: on or off

Default: on


image_filter_sharpen

Context: http, server, location

Sharpens the image by specified percentage (value may exceed 100).

Syntax: Numeric value

Default: 0


Please note that when it comes to JPG images, Nginx automatically strips off metadata (such as EXIF) if it occupies more than 5 percent of the total space of the file.

XSLT

The Nginx XSLT module allows you to apply an XSLT transform on an XML file or response received from a backend server (proxy, FastCGI, and so on) before serving the client.

This module is not included in the default Nginx build.


xml_entities

Context: http, server, location

Specifies the DTD file containing symbolic element definitions.

Syntax: File path

Example: xml_entities xml/entities.dtd;


xslt_stylesheet

Context: location

Specifies the XSLT template file path with its parameters. Variables may be inserted in the parameters.

Syntax: xslt_stylesheet template [param1] [param2…];

Example: xslt_stylesheet xml/sch.xslt param=value;


xslt_types

Context: http, server, location

Defines additional MIME types to which the transforms may apply, other than text/xml.

Syntax: MIME type

Example:

xslt_types text/xml text/plain;
xslt_types *;


xslt_param xslt_string_param

Context: http, server, location

Both directives allow defining parameters for XSLT stylesheets. The difference lies in the way the specified value is interpreted: using xslt_param, XPath expressions in the value are processed; while xslt_string_param should be used for plain character strings.

Syntax: xslt_param key value;


Nginx - Additional Modules, Content and Encoding的更多相关文章

  1. Nginx - Additional Modules, Website Access and Logging

    The following set of modules allows you to configure how visitors access your website and the way yo ...

  2. Nginx - Additional Modules, Limits and Restrictions

    The following modules allow you to regulate access to the documents of your websites — require users ...

  3. Nginx - Additional Modules, About Your Visitors

    The following set of modules provides extra functionality that will help you find out more informati ...

  4. Nginx - Additional Modules, SSL and Security

    Nginx provides secure HTTP functionalities through the SSL module but also offers an extra module ca ...

  5. nginx---reference

    nginx (pronounced "engine x") is a free open source web server written by Igor Sysoev, a R ...

  6. 使用wordpress搭建自己的独立博客

    最近想要搭建自己的私人博客, 各种百度,完整的搭建步骤如下! 首先得要有自己的vps或者云主机,我这里是自己的云主机,有自己的域名(我这边目前没有买域名)! 搭建步骤! 1,安装lnmp(linux+ ...

  7. 搭建 WordPress 博客教程

    搭建 WordPress 博客教程(超详细) 在 2018年7月29日 上张贴 由 suncent一条评论 本文转自:静候那一米阳光 链接:https://www.jianshu.com/p/5675 ...

  8. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  9. Table of Contents - Nginx

    Downloading and  Installing Nginx Nginx for Windows Basic Nginx Configuration Configuration File Syn ...

随机推荐

  1. Socket连接与HTTP连接

    我们在传输数据时,可以只使用(传输层)TCP/IP协议,但是那样的话,如果没有应用层,便无法识别数据内容,如果想要使传输的数据有意义,则必须使用到应用层协议,应用层协议有很多,比如HTTP.FTP.T ...

  2. 简单http笔记

    https是以安全为目的的网络传输协议,可以认为是http的安全版,https使用ssl协议保证安全传输.https位于网络模型的应用层,使用默认端口443进行通信,URL以https开头是https ...

  3. no datanode to stop

    昨晚整了半天,遇上的问题是通过start-all.sh无法启动datanode,然后关闭时就会报no datanode to stop ,引起这个的原因是因为我多次格式化,导致namespaceID不 ...

  4. 解决IntelliJ IDEA 13更新FindBugs 0.9.993时JRE版本过低导致启动失败问题

    今晚更新FindBugs 0.9.992(FindBugs 2)至FindBugs 0.9.993(FindBugs 3)后,按要求重启IntelliJ IDEA 13.本想看看更新后多了哪些功能,结 ...

  5. 使用charles 抓包

    1.保证手机和Mac连接的是同一个无线局域网 2.在Mac上打开Charles Proxy,菜单栏操作:Proxy→Proxy Setting,检查端口,一般保持默认,HTTP Proxy端口为888 ...

  6. POJ1463:Strategic game(树形DP)

    Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...

  7. 详谈 php定时器

    以前对se特别感兴趣,但是自己又不会java,lucene等搜索引擎开发工具,于是不断挖掘php的功效. 最后发现php也可以做抓取,并且原理很易:直接获取页面源文件,然后通过正则或字符串的参照截取来 ...

  8. 贪心算法(Greedy Algorithm)之最小生成树 克鲁斯卡尔算法(Kruskal's algorithm)

    克鲁斯卡尔算法(Kruskal's algorithm)是两个经典的最小生成树算法的较为简单理解的一个.这里面充分体现了贪心算法的精髓.大致的流程能够用一个图来表示.这里的图的选择借用了Wikiped ...

  9. 更新mac自带的python

    mac lion自带的python是2.7版本的,有点儿低,要使用一些应用的时候还需要3.3以上的,这时可以用以下的方法更新mac自带的python. 先去python的官网下载最新的python,并 ...

  10. online ddl 跟踪

    | >mysql_prepare_alter_table T@4 : | | | | | | <mysql_prepare_alter_table 7368 T@4 : | | | | | ...