诺亚方舟

沉淀

nginx之HTTP模块

 

默认值:no 使用环境:location 用法:

1
2
3
location /test/ {
alias /data/web/test/;
}

注意:alias与root的不同在于,匹配到的uri并不会在指定的目录继续展开下去,例如上面匹配到的整个uri则直接指定到/data/web/test/目录下而不是/data/web/test/test/

  • client_header_timeout

默认值:60 使用环境:http,server 用法:

1
2
3
http{
client_header_timeout 10;
}

注意: 该指令用于设置读取客户端请求head的超时,一次请求中,进入请求头的读取步骤时,如果超过这个值客户端仍没有发送任何数据,那么nginx将返回“Request time out(408)”

  • client_body_timeout

默认值:60 使用环境:http,server,location 用法:

1
2
3
location /test/ {
client_body_timeout 10;
}

注意:该指令用于设置读取超时,指的是一个请求的请求体还没有进入请求体读取步骤时的超时,如果超过这个时间客户端仍没发送任何数据,服务端将返回“Request time out(408)”

  • client_max_body_size

默认值:1m 使用环境:http,server,location 用法:

1
2
3
location /test/ {
client_max_body_size 1m;
}

注意:该指令用来设置http请求体的最大值,也就是客户端请求中Content-Length 这个字段的值,如果请求体大于这个值将会返回“Request Entity Too Large(413)” .

  • error_page

默认值:no 使用环境:http,server,location,If in location 用法:

1
2
3
4
5
6
7
8
location /test/ {
error_page 404 /404.html;
error_page 502 503 504 /50x.html;
error_page 403 http://iamjs.net:8888/index.html;
error_page 404 = @fetch;//跳到@fetch的location模块
error_page 500 = 200 /error.php;//修改状态码并访问某个程序
error_page 499 = /record.php;//不修改状态码访问某个程序
}

注意:如上面例子,error_page这个指令有多种写法,当出现异常状态码(非200)时,可以通过这个指令来修改状态码、或者访问某个静态页面、某个自己写好的统计程序等等。

  • internal

默认值:no 使用环境:location 用法:

1
2
3
location /404.html {
internal;
}

注意:在location出现这个指令,代表着这个location只能通过“内部”请求访问,内部请求包括 1)使用error_page重定向; 2)由“ngx_http_ssi_module”模块的include virtual指令创建的字请求; 3)由“ngx_http_rewrite_module”模块的rewrite指令改变的请求方向。

  • keepalive_timeout

默认值:keepalive_time 75 使用环境:http,server,location 用法:

1
2
3
location /test/ {
keepalive_time 75;
}

注意:该指令有两个参数,第一个参数用于设定客户端的keep-alive链接超时,在这个时间过后服务器将会关闭链接。第二个选项是可选项,他的值决定了响应头Keep-Alive:timeout=”time”的值,这个头能够告诉一些浏览器关闭链接,这样服务器就不用再次发送关闭链接的包请求了。

  • keepalive_requests

默认值:keepalive_requests 100 使用环境:http,server,location 用法:

1
2
3
location /test/ {
keepalive_requests 200;
}

注意:该指令用于设置nginx保持活跃的连接数。

  • limit_except

默认值:no 使用环境:location 用法:

1
2
3
4
location GET {
allow 192.168.1.0/32;
deny all;
}

注意:该指令用于限制访问location的HTTP方法。

  • lingerlng_time

默认值:30s 使用环境:http,server,location 用法:

1
lingerlng_time 30s

注意:当客户端上传的数据超过nginx设置的max_client_body_size的值,nginx就会发送“413 Request entity too large”的错误信息,该指令用于设置nginx发送错误响应到关闭连接之间的时间。

  • lingerlng_timeout

默认值:5s 使用环境:http,server,location 用法:

1
2
3
4
5
6
7
location ~.*\.php$
{
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
 
}

注意:该指令定义了nginx在客户端关闭之前,两个读操作之间的等待时间。

  • listen

默认值:80

使用环境:server

用法:

1
2
3
4
5
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen locaohost:8000;

注意:listen指令可以设置为监听某个端口,或者某个ip/域名的端口访问。listen的default_server参数可以设置该server区段为默认服务器配置,其他参数详见百度。

  • location

默认值:no

使用环境:server

用法:

1
2
3
4
5
6
location ~ .*\.php${
include fcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
expires off;
}

注意:要决定哪一个location指令定义的值能够匹配某一访问,首先是按照字面字符串检查,即按照最明确的匹配查询,然后就是正则表达式的检测,对于正则表达式的检测是按照配置文件中的顺序进行检测的,第一个匹配正则表达式的location将会被使用,并停止搜索,如果没有正则表达式匹配,那么按照字面字符串搜索到的location将会被使用。

  • root

默认值:html

使用环境:http,server,location,location中的If

用法:

1
2
3
location /i/{
root /spool/w3;
}

注意:该指令指定了一个其你去的根文档目录。

  • server

默认值:no

使用环境:http

注意:用于配置虚拟主机。

  • server_name

默认值:‘’

使用环境:server

用法:

1
2
3
server / {
server_name example.com *.example.com;
}

注意:支持使用正则匹配服务器名称,可以用’*‘来替代域名的第一部分和最后一部分。0.7.12版本以后支持空的服务器名字,也就是可以捕获到http请求中host为空的请求。

nginx匹配http请求与服务器名顺序:

①,匹配全域名,静态域名,即直接匹配server_name指令;

②,开始部分使用通配符的域名,如:*.example.com;

③,结尾部分使用通配符的域名,如:www.example.*;

④,带有正则表达式的域名。

⑤,当没有找到匹配的server_name时,匹配listen指令标记为default_server的server区段;

⑥,匹配listen(或隐含有listen 80)指令的第一个server区段。

  • log_not_found

默认值:on

使用环境:http,server,location

用法:

1
log_not_found on;

注意:该指令用于设置nginx找不到文件时是否向error_log发送日志。

  • post_action

默认值:off

使用环境:http,server,location,If-in-location

用法:

1
2
3
4
5
6
7
8
9
10
location /protected_files{
internal;
proxy_pass http://127.0.0.2;
post_action /protected_done;
}
 
location /protected_done{
internal;
fastcgi_pass 127.0.0.1:9000;
}

注意:定义了匹配到的这个请求结束之后,nginx会调用的URI。

  • recursive_error_pages

默认值:off

使用环境:http,server,location,If-in-location

用法:

1
recirsive_error_pages off;

注意:有时候指令error_page提供的错误页面本身也发生了错误,这个指令用于设置是否会再次递归error_page指令。

  • reset_timedout_connection

默认值:off

使用环境:http,server,location

用法:

1
reset_timedout_connection on;

注意:客户端链接超时时,请求的相关信息可能还保留在内存中,这个选项开启后会清除所有与内存的关联。

  • resolver

默认值:no

使用环境:http,server,location

用法:

1
resolver 127.0.0.1;

注意:用于设置DNS服务器。

  • resolver_timeout

默认值:30s

使用环境:http,server,location

用法:

1
resolver_timeout 5s;

注意:用于设置DNS域名解析超时时间。

  • satisfy

默认值:all

使用环境:http,server,location

用法:

1
2
3
4
5
6
7
location / {
satisfy any/all;
allow 192.168.1.0/32;
deny all;
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}

注意:上面的例子中,通过allow和deny指令只允许本地ip的客户端,auth_basic和auth_basic_user_file指令只允许能够提供用户名和密码的用户。使用satisfy all则要同时满足两个条件才能访问,使用satisfy any只要满足其中一种条件即可访问。

  • send_timeout

默认值:60

使用环境:http,server,location

用法:

1
2
3
location / {
send_timeout 60;
}

注意:当链接处于不活动状态时,该指令用于设置nginx两个读请求等待的时间,如果这段时间内客户端没有进行任何操作,nginx将会关闭链接。

  • sendfile

默认值:off

使用环境:http,server,location

用法:

1
2
3
location / {
sendfile on;
}

注意:该指令用于设置是否使用sendfile(),这种方法是在内核中操作的,因此笔read(2)和wirte(2)的协作操作更高效,这是由于read、write有个从用户控件传递数据的过程。

 

  • try_files

默认值:none

使用环境:server,location

用法:

1
2
3
4
5
6
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location / {
try_files $uri $uri/ =404;
}

注意:该指令用于按顺序检测文件的存在性,并返回找到的第一个文件。如果列表中找不到文件,那么最后一个参数(一个内部重定向)将会被使用,另外,这个参数可以用http状态码来替代。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>