Nginx 基本指令整理1

  categories:nginx  author:

原创文章,转载请指明出处并保留原文url地址

 

本文主要针对nginx的几个主要基础指令做一个简单的整理,另外部分指令做了实际测试,本文具体包括如下指令:

aioaliaschunked_transfer_encodingclient_body_buffer_sizeclient_body_in_file_onlyclient_body_in_single_bufferclient_body_temp_path

1. aio 

syntax:

aioon | off | sendfile;

default:

aio off;

context:

http, server, location

This directive appeared in version 0.8.11.

这个指令在nginx0.81版本以后出现

指令的功能:在FreeBSD或者Linux启用或禁用异步文件I / OAIO
这个指令同操作系统内核有很密切的关系,建议熟悉这个指令并且熟悉相关操作系统下在进行处理,否则建议才用默认值。

原文如下:Enables or disables the use of asynchronous file I/O (AIO) on FreeBSD and Linux.

On FreeBSD, AIO is usable starting from FreeBSD 4.3. AIO can either be linked statically into a kernel:

    options VFS_AIO

or loaded dynamically as a kernel loadable module:

    kldload aio

In FreeBSD versions 5 and 6, enabling AIO statically, or dynamically when booting the kernel, will cause the entire networking subsystem to use the Giant lock that can impact overall performance negatively. This limitation has been removed in FreeBSD 6.4-STABLE in 2009, and in FreeBSD 7. However, starting from FreeBSD 5.3 it is possible to enable AIO without the penalty of running the networking subsystem under a Giant lock – for this to work, the AIO module needs to be loaded after the kernel has booted. In this case, the following message will appear in /var/log/messages

    WARNING: Network stack Giant-free, but aio requires Giant.

    Consider adding ‘options NET_WITH_GIANT’ or setting debug.mpsafenet=0

and can safely be ignored.

    The requirement to use the Giant lock with AIO is related to the fact that FreeBSD supports asynchronous calls aio_read() and aio_write() when working with sockets. However, since nginx only uses AIO for disk I/O, no problems should arise.

For AIO to work, sendfile needs to be disabled:

    location /video/ {

        sendfile       off;

        aio            on;

        output_buffers 1 64k;

    }

In addition, starting from FreeBSD 5.2.1 and nginx 0.8.12, AIO can also be used to pre-load data for sendfile():

    location /video/ {

        sendfile       on;

        tcp_nopush     on;

        aio            sendfile;

    }

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

On Linux, AIO is usable starting from kernel version 2.6.22; plus, it is also necessary to enable directio, otherwise reading will be blocking:

    location /video/ {

        aio            on;

        directio       512;

        output_buffers 1 128k;

    }

On Linux, directio can only be used for reading blocks that are aligned on 512-byte boundaries (or 4K for XFS). Reading of unaligned files end is still made in blocking mode. The same holds true for byte range requests, and for FLV requests not from the beginning of a file: reading of unaligned data at the beginning and end of a file will be blocking. There is no need to turn off sendfile explicitly as it is turned off automatically when directio is used.

2. alias

syntax:

alias  path;

default:

context:

location

针对location中的位置指定一个别名,访问location中相关信息是相当于访问笔名的位置,详情参考:实验确认nginx rootaliaslocation指令使用方法

Nginx官方解释如下:

Defines a replacement for the specified location. For example, with the following configuration    location /i/ {

        alias /data/w3/images/;

    }

the request of “/i/top.gif” will be responded with the file /data/w3/images/top.gif.

The path value can contain variables except $document_root and $realpath_root.

If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:

    location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {

        alias /data/w3/images/$1;

    }

When location matches the last part of the directive’s value:

    location /images/ {

        alias /data/w3/images/;

    }

it is better to use the root directive instead:

    location /images/ {

        root /data/w3;

    }

3. chunked_transfer_encoding

syntax:

chunked_transfer_encoding on | off;

default:

chunked_transfer_encoding on;

context:

http, server, location

允许或者禁止http1.1中的 chunked transfer encoding编码方式。

一般HTTP通信时,会使用Content-Length头信息性来通知用户浏览器服务器发送的文档内容长度。浏览器接收到此头信息后,接受完Content-Length中定义的长度字节后开始解析页面,但如果服务端有部分数据延迟发送则会出现浏览器白屏,造成比较糟糕的用户体验。

解决方案是在HTTP1.1协议中定义的Transfer-Encoding: chunked的头信息所有HTTP1.1 应用都支持此使用trunked编码动态的提供body内容的长度的方式。进行Chunked编码传输的HTTP数据要在消息头部设 置:Transfer-Encoding: chunked表示Content Body将用chunked编码传输内容。根据定义,浏览器不需要等到内容字节全部下载完成,只要接收到一个chunked块就可解析页面.并且可以下载 html中定义的页面内容,包括js,css,image等。

详情参考:HTTP1.1CHUNKED编码解析

Nginx官方解释如下:

Allows disabling chunked transfer encoding in HTTP/1.1. It may come in handy when using a software failing to support chunked encoding though the standard requires it.

4. client_body_buffer_size

syntax:

client_body_buffer_sizesize;

default:

client_body_buffer_size 8k|16k;

context:

http, server, location

设置读取客户端请求的body时的缓存大小。在某些情况下当前请求的body大于缓存尺寸时,body将被缓存到本地磁盘的临时文件中。缺少情况下缓冲区大小是两个内存页的大小。

注临时文件位置下面有专用指令设置,缓存是否长久保留也可以通过另外一个指令设置,稍后介绍。

Nginx官方解释如下:

Sets buffer size for reading client request body. In case request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms.

5. client_body_in_file_only

syntax:

client_body_in_file_onlyon | clean | off;

default:

client_body_in_file_only off;

context:

http, server, location

确定nginx是否应该保存整个客户端请求的body在文件里。这个指令能被用来在调试情况下等。

Nginx官方解释如下:

Determines whether nginx should save the entire client request body into a file. This directive can be used during debugging, or when using the $request_body_file variable, or the $r->request_body_file method of the module ngx_http_perl_module.

When set to the value on, temporary files are not removed after request processing.

The value clean will cause the temporary files left after request processing to be removed.

6. client_body_in_single_buffer

syntax:

client_body_in_single_buffer    on | off;

default:

client_body_in_single_buffer off;

context:

http, server, location

确定nginx是否应该保存整个客户端请求的body在单个缓存中。这个指令被推荐使用当用户使用“$request_body”变量时。

Nginx官方解释如下:

Determines whether nginx should save the entire client request body in a single buffer. The directive is recommended when using the $request_body variable, to save the number of copy operations involved.

7. client_body_temp_path

syntax:

client_body_temp_path path[level1 [level2 [level3]]];

default:

client_body_temp_path client_body_temp;

context:

http, server, location

定义一个存储客户端请求body临时文件目录,3级子目录的层次结构,可以使用下面指定的目录。例如,在下面的配置
   client_body_temp_path  /spool/nginx/client_temp  1   2;
一个临时文件可能看起来像这样:
    / spool/nginx/client_temp/7/45/00000123457

Nginx官方解释如下:

Defines a directory for storing temporary files holding client request bodies. Up to three-level subdirectory hierarchy can be used underneath the specified directory. For example, in the following configuration

    client_body_temp_path /spool/nginx/client_temp 1 2;

a temporary file might look like this:

    /spool/nginx/client_temp/7/45/00000123457

下面是个记录客户端请求body的例子,供参考

[root@s02 nginx]# cat nginx.conf

user  root;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr – $remote_user [$time_local] "$request" ‘

                      ‘$status $body_bytes_sent "$http_referer" ‘

                      ‘"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access1.log  main;

    client_body_in_file_only on;

    client_body_temp_path /test/nginx/debug 1 2;

    sendfile        on;

    tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    gzip  on;

    include vhost/t.conf;

}

文件目录如下:

[root@s02 debug]# pwd

/test/nginx/debug

[root@s02 debug]# tree

.

|– 1

|   `– 00

|       `– 0000000001

|– 2

|   `– 00

|       `– 0000000002

|– 3

|   `– 00

|       `– 0000000003

`– 4

    `– 00

        `– 0000000004

8 directories, 4 files

[root@s02 debug]#

文件内容:

[root@s02 00]# pwd

/test/nginx/debug/1/00

[root@s02 00]# ls

0000000001

[root@s02 00]# cat 0000000001

aaa=1&b=3

[root@s02 00]#

原创文章,转载请指明出处并保留原文url地址



快乐成长 每天进步一点点