nginx基本指令整理3

  categories:nginx  author:

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

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

directio,directio_alignment,disable_symlinks,disable_symlinks,error_page,etag

14. directio

syntax:

Directio   size| off;

default:

directio off;

context:

http, server, location

This directive appeared in version 0.7.7.

FreeBSD或者Linux启用O_DIRECT标记的直接磁盘io操作Mac OS启用 F_NOCACHE标志的磁盘操作,Solarisdirectio()函数在读取文件时,若是给定文件大小大于等于指定尺寸时,自动禁止sendfile操作。这个行为有可能对大文件操作有好处

直接iodirectio情况如下:

对于传统的操作系统来说,普通的 I/O 操作一般会被内核缓存,这种 I/O 被称作缓存 I/OO_DIRECT IO 直接IO文件访问机制不经过操作系统内核的缓存,数据直接在磁盘和应用程序地址空间进行传输,所以该文件访问的机制称作为直接 I/OLinux 中就提供了这样一种文件访问机制,对于那种将 I/O 缓存存放在用户地址空间的应用程序来说,直接 I/O 是一种非常高效的手段。

直接 I/O 潜在可能存在的问题,O_DIRECT I/O 并不一定总能提供令人满意的性能上的飞跃。设置直接 I/O 的开销非常大,而直接 I/O 又不能提供缓存 I/O 的优势。缓存 I/O 的读操作可以从高速缓冲存储器中获取数据,而直接 I/O 的读数据操作会造成磁盘的同步读,这会带来性能上的差异 , 并且导致进程需要较长的时间才能执行完;对于写数据操作来说,使用直接 I/O 需要 write() 系统调用同步执行,否则应用程序将会不知道什么时候才能够再次使用它的 I/O 缓冲区。与直接 I/O 读操作类似的是,直接 I/O 写操作也会导致应用程序关闭缓慢。所以,应用程序使用直接 I/O 进行数据传输的时候通常会和使用异步 I/O 结合使用。 

详情参考 Linux 中直接 I/O 机制的介绍

Nginx官方原文如下:

Enables the use of the O_DIRECT flag (FreeBSD, Linux), the F_NOCACHE flag (Mac OS X), or the directio() function (Solaris), when reading files that are larger than or equal to the specified size. It automatically disables (0.7.15) the use of sendfile for a given request. It could be useful for serving large files:

    directio 4m;

or when using aio on Linux.

15. directio_alignment

syntax:

directio_alignment  size;

default:

directio_alignment 512;

context:

http, server, location

This directive appeared in version 0.8.11.

设置的directio的对齐方式。在大多数情况下,一个512字节的对齐方式是足够的。但Linux下使用XFS时,需要增加至4K

Nginx官方解释如下:

Sets an alignment for directio. In most cases, a 512-byte alignment is enough, however, when using XFS under Linux, it needs to be increased to 4K.

16. disable_symlinks

syntax:

disable_symlinks  off;
disable_symlinks  on | if_not_owner [from=part];

default:

disable_symlinks off;

context:

http, server, location

This directive appeared in version 1.1.15.

client_body_timeout 非常相似,仅仅这次是读取用户的请求头,而不是body

Nginx官方解释如下:

Defines a timeout for reading client request header. If a client does not transmit the entire header within this time, the client error 408 (Request Time-out) is returned.

17. disable_symlinks

syntax:

disable_symlinks   off;
disable_symlinks   on | if_not_owner [from=part];

default:

disable_symlinks off;

context:

http, server, location

This directive appeared in version 1.1.15.

确定当打开文件时,符号连接文件如何被处理。
Off: 默认行为,符号链接的路径名是允许的,并且不做检查
ON: 若是文件路径中任何一部分是符号连接文件,则存取被拒绝。

if_not_owner:若是存取的文件路径中包括符号连接文件,但是在连接中的对象里面有不同的用户属主出现(就是多个对象不是同一个用户的(owner))的则决绝这个文件的操作等

其他的没用用过,大家自己看原文吧.

Nginx官方解释如下:

Determines how symbolic links should be treated when opening files:

Off    Symbolic links in the pathname are allowed and not checked. This is the default behavior.

on    If any component of the pathname is a symbolic link, access to a file is denied.

if_not_owner     Access to a file is denied if any component of the pathname is a symbolic link, and the link and object that the link points to have different owners.

from=part    When checking symbolic links (parameters on and if_not_owner), all components of the pathname are normally checked. Checking of symbolic links in the initial part of the pathname may be avoided by also specifying the from=part parameter. In this case, symbolic links are checked only from the component of the pathname following the specified initial part. If a value is not an initial part of the checked pathname, the whole pathname is checked as if this parameter was not specified at all. If a value fully matches the file name, symbolic links are not checked. The parameter value can contain variables.

Example:

    disable_symlinks on from=$document_root;

This directive is only available on systems that have the openat() and fstatat() interfaces. This includes modern versions of FreeBSD, Linux, and Solaris.

Parameters on and if_not_owner add a processing overhead.

On systems that do not support opening directories for search only, the use of these parameters requires that worker processes have read permissions for all checked directories.

The ngx_http_autoindex_module, ngx_http_random_index_module, and ngx_http_dav_module modules currently ignore this directive.

18. error_page

syntax:

error_page    code… [=[response]]   uri;

default:

context:

http, server, location, if in location

为特定的错误定义一些URI显示相关信息

若当前级别的定义中没有相关错误指令的定义等,将继承上一个级别的错误指令的定义,若是当前级别中有了定义,则不进行继承。

示例:
    error_page404 /404.html;
    error_page500 502 503 504  /50x.html;

此外,它是还可以使用使用“=响应的语法,例如:
    error_page404 =200 / empty.gif;
具体配置有很多,参考原文。

Nginx官方解释如下:

Defines the URI that will be shown for the specified errors. These directives are inherited from the previous level if and only if there are no error_page directives on the current level. A uri value can contain variables.

Example:

    error_page 404             /404.html;

    error_page 500 502 503 504 /50x.html;

Furthermore, it is possible to change the response code to another using the =responsesyntax, for example:

    error_page 404 =200 /empty.gif;

If an error response is processed by a proxied server, or a FastCGI server, and the server may return different response codes (e.g., 200, 302, 401 or 404), it is possible to respond with a returned code:

    error_page 404 = /404.php;

It is also possible to use redirects for error processing:

    error_page 403      http://example.com/forbidden.html;

    error_page 404 =301 http://example.com/notfound.html;

In this case, the response code 302 is returned to the client. It can only be changed to one of the redirect status codes (301, 302, 303, and 307).

If there is no need to change URI during internal redirection it is possible to pass error processing into a named location:

    location / {

        error_page 404 = @fallback;

    }

    location @fallback {

        proxy_pass http://backend;

    }

    If uri processing leads to an error, the status code of the last occurred error is returned to the client.

19. etag

syntax:

Etag  on | off;

default:

Etag  on;

context:

http, server, location

This directive appeared in version 1.3.3.

 启用或禁用针对静态资源自动生成的“ETag响应头字段

ETag信息参考如下:HTTP协议缓存策略深入详解之etag妙用

Nginx官方解释如下:

Enables or disables automatic generation of the “ETag” response header field for static resources.



快乐成长 每天进步一点点