1. 程式人生 > >Nginx作為靜態資源web服務_配置語法

Nginx作為靜態資源web服務_配置語法

Nginx作為靜態資源web服務_配置語法

1、配置語法-sendfile檔案讀取

(1)核心作用

        提高檔案檔案傳輸效率。

(2)sendfile語法

Syntax: sendfile on | off;
Default:
sendfile off;
Context: httpserverlocationif in location

 

語法解釋:

Enables or disables the use of sendfile().

Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():

location /video/ {
    sendfile       on;
    tcp_nopush     on;
    aio            on;
}

In this configuration, sendfile() is called with the SF_NODISKIO flag which causes it not to block on disk I/O, but, instead, report back that the data are not in memory. nginx then initiates an asynchronous data load by reading one byte. On the first read, the FreeBSD kernel loads the first 128K bytes of a file into memory, although next reads will only load data in 16K chunks. This can be changed using the

read_ahead directive.

Before version 1.7.11, pre-loading could be enabled with aio sendfile;.

2、配置語法-tcp_nopush

(1)核心作用

        sendfile開啟的情況下,提高網路包的傳輸效率。

(2)tcp_nopush語法

Syntax:

tcp_nopush on | off;

Default:

tcp_nopush off;

Context:

http, server, location

 

語法解釋:

Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows

  • sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;
  • sending a file in full packets.

3、配置語法-tcp_nodelay

(1)核心作用

        keepalive連線下,提高網路包的傳輸實時性。

(2)tcp_nodelay語法

Syntax:

tcp_nodelay on | off;

Default:

tcp_nodelay on;

Context:

http, server, location

 

語法解釋:

Enables or disables the use of the TCP_NODELAY option. The option is enabled when a connection is transitioned into the keep-alive state. Additionally, it is enabled on SSL connections, for unbuffered proxying, and for WebSocket proxying.

4、配置語法-gzip壓縮

(1)核心作用

       壓縮傳輸,提高傳輸效率。

(2)gzip語法

Syntax:

gzip on | off;

Default:

gzip off;

Context:

http, server, location, if in location

語法解釋:

Enables or disables gzipping of responses.

 

Syntax:

gzip_buffers number size;

Default:

gzip_buffers 32 4k|16 8k;

Context:

http, server, location

語法解釋:

Sets the number and size of buffers used to compress a response. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform.

Until version 0.7.28, four 4K or 8K buffers were used by default.

 

Syntax:

gzip_comp_level level;

Default:

gzip_comp_level 1;

Context:

http, server, location

語法解釋:

Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.

 

Syntax:

gzip_disable regex ...;

Default:

Context:

http, server, location

This directive appeared in version 0.6.23.

語法解釋:

Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.

The special mask “msie6” (0.7.12) corresponds to the regular expression “MSIE [4-6]\.”, but works faster. Starting from version 0.8.11, “MSIE 6.0; ... SV1” is excluded from this mask.

 

Syntax:

gzip_http_version 1.0 | 1.1;

Default:

gzip_http_version 1.1;

Context:

http, server, location

語法解釋:

Sets the minimum HTTP version of a request required to compress a response.

 

Syntax:

gzip_min_length length;

Default:

gzip_min_length 20;

Context:

http, server, location

語法解釋:

Sets the minimum length of a response that will be gzipped. The length is determined only from the “Content-Length” response header field.

 

 

Syntax:

gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;

Default:

gzip_proxied off;

Context:

http, server, location

語法解釋:

Enables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the “Via” request header field. The directive accepts multiple parameters:

off

disables compression for all proxied requests, ignoring other parameters;

expired

enables compression if a response header includes the “Expires” field with a value that disables caching;

no-cache

enables compression if a response header includes the “Cache-Control” field with the “no-cache” parameter;

no-store

enables compression if a response header includes the “Cache-Control” field with the “no-store” parameter;

private

enables compression if a response header includes the “Cache-Control” field with the “private” parameter;

no_last_modified

enables compression if a response header does not include the “Last-Modified” field;

no_etag

enables compression if a response header does not include the “ETag” field;

auth

enables compression if a request header includes the “Authorization” field;

any

enables compression for all proxied requests.

 

Syntax:

gzip_types mime-type ...;

Default:

gzip_types text/html;

Context:

http, server, location

語法解釋:

Enables gzipping of responses for the specified MIME types in addition to “text/html”. The special value “*” matches any MIME type (0.8.29). Responses with the “text/html” type are always compressed.

 

 

Syntax:

gzip_vary on | off;

Default:

gzip_vary off;

Context:

http, server, location

語法解釋:

Enables or disables inserting the “Vary: Accept-Encoding” response header field if the directivesgzipgzip_static, or gunzip are active.

 

                                                            Embedded Variables

$gzip_ratio

achieved compression ratio, computed as the ratio between the original and compressed response sizes.

 

(3)gzip壓縮傳輸圖

        

      Nginx壓縮的資源,瀏覽器自動解壓縮。

 

5、配置語法-gzip_static

(1)核心作用

           傳輸預壓縮靜態檔案給客戶端(.gz檔案為預壓縮)

(2)gzip_static語法

Syntax:

gzip_static on | off | always;

Default:

gzip_static off;

Context:

http, server, location

語法解釋:

Enables (“on”) or disables (“off”) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_versiongzip_proxiedgzip_disable, and gzip_vary.

With the “always” value (1.3.6), gzipped file is used in all cases, without checking if the client supports it. It is useful if there are no uncompressed files on the disk anyway or the ngx_http_gunzip_module is used.

The files can be compressed using the gzip command, or any other compatible one. It is recommended that the modification date and time of original and compressed files be the same.

(3)gzip_static舉例

    location ~ /download {

      gzip_static  on;

            gzip_proxied expired no-cache no-store private auth;

            root /opt;

    }