ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

一些常用的ngx_http_core_module介绍

2020-09-09 16:32:37  阅读:218  来源: 互联网

标签:body core http module server location file open


alias path

alias path: 路径别名,,文档映射的另一种机制;仅能用于location上下文

Syntax:    alias path;
Default:    —
Context: location

案例:

http://www.magedu.com/bbs/index.html

location /bbs { 注意: /bbs 后建议不要加 /
alias /web/forum/;
} 
访问时得到 /web/forum/index.html页面的信息


location /bbs/ {
root /web/forum/;
}
访问时得到 /web/forum/bbs/index.html


注意:location中使用root指令和alias指令的意义不同
(a) root,给定的路径对应于location中的/uri 左侧的/
(b) alias,给定的路径对应于location中的/uri 的完整路径
总结
root 是全路径。拼接 root 和 location 设置的路径
alias 相当于重置路径,重置为 alias 设置的路径;去掉了location后的路径,只要alias的路径

index file...

指定默认网页文件;定义错误页,以指定的响应状态码进行响应

Syntax:    error_page code ... [=[response]] uri;
Default:    —
Context:    http, server, location, if in location

案例

error_page 404 /404.html;
      location = /40x.html {
 }
 
#error_page 404 =200 /404.html;

error_page 500 502 503 504 /50x.html;
       location = /50x.html {
 }

生产案例:

listen 80;
server_name www.magedu.net;
error_page 500 502 503 504 404 /error.html;
location = /error.html {
root /data/nginx/html;
}

try_files file ... uri;

try_files file ... =code; 按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向

Syntax:    try_files file ... uri;
try_files file ... =code;
Default:    —
Context:    server, location

案例

location /images/ {
try_files $uri /images/default.jpg;
} 说明:/images/default.jpg 为 URI

location / {
try_files $uri $uri/index.html $uri.html =404;
}
location / {
            try_files $uri $uri/ /index.php?$query_string;

}

#当用户请求 http://localhost/example 时,这里的 $uri 就是 /example。 
#try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是项目代码安装目录)的文件,就直接把这个文件的内容发送给用户。 
#显然,目录中没有叫 example 的文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。 
#又找不到,就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到 http://localhost/index.php。 
loaction / {

try_files $uri @apache

}

loaction @apache{

proxy_pass http://127.0.0.1:88

include aproxy.conf

}

#try_files方法让Ngxin尝试访问后面得$uri链接,并进根据@apache配置进行内部重定向。

#当然try_files也可以以错误代码赋值,如try_files /index.php = 404 @apache,则表示当尝试访问得文件返回404时,根据@apache配置项进行重定向。

定义客户端请求的相关配置

keepalive_timeout timeout [header_timeout];

设定保持连接超时时长,0表示禁止长连接,默认为75

Syntax:    keepalive_timeout timeout [header_timeout];
Default:    
keepalive_timeout 75s;
Context:    http, server, location
设定保持连接超时时长,0表示禁止长连接,默认为75s
示例:在响应头显示此首部字段
keepalive_timeout 60 60;

keepalive_requests number;

在一次长连接上所允许请求的资源的最大数量,默认为100

Syntax:    keepalive_requests number;
Default:    
keepalive_requests 100;
Context:    http, server, location
在一次长连接上所允许请求的资源的最大数量,默认为100
keepalive_requests number 1000

keepalive_disable none | browser ...;

对哪种浏览器禁用长连接

Syntax:    keepalive_disable none | browser ...;
Default:   keepalive_disable msie6;
Context:    http, server, location
对哪种浏览器禁用长连接
keepalive_disable google; #禁止google浏览器长连接

send_timeout time;

向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非整个响应过程的传输时长

Syntax:    send_timeout time;
Default:    send_timeout 60s;
Context:    http, server, location

client_max_body_size size;

求报文中实体的最大值,设为0,则不限制,默认1m,超过报413错误

Syntax:    client_max_body_size size;
Default:    client_max_body_size 1m;
Context:    http, server, location

client_body_buffer_size size;

用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置

Syntax:    client_body_buffer_size size;
Default:    client_body_buffer_size 8k|16k;
Context:    http, server, location

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

设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量 目录名为16进制的数字;用hash之后的值从后往前截取第1、2、3级作为文件名

client_body_temp_path /var/tmp/client_body 1 2 2

1 1级目录占1位16进制,即2^4=16个目录 0-f

2 2级目录占2位16进制,即2^8=256个目录 00-ff

2 3级目录占2位16进制,即2^8=256个目录 00-ff

Syntax:    client_body_temp_path path [level1 [level2 [level3]]];
Default:    client_body_temp_path client_body_temp;
Context:    http, server, location

案例

上传服务器配置生产案例:
location /upload {
  keepalive_timeout 60 60;
  keepalive_requests number 1000;
  keepalive_disable google;
  send_timeout 120s;
  client_max_body_size 100m;
  client_body_buffer_size 2048k;
  client_body_temp_path /apps/nginx/temp 1 2 2;
}

对客户端的相关设置

limit_rate rate;

限制响应给客户端的传输速率,单位是bytes/second

默认值0表示无限制

限制向客户端传输响应的速率。在rate被以每秒字节数指定。零值禁用速率限制。该限制是根据请求设置的,因此,如果客户端同时打开两个连接,则总速率将是指定限制的两倍。

Syntax:    limit_rate rate;
Default:    limit_rate 0;
Context:    http, server, location, if in location

limit_except method;

限制客户端使用除了指定的请求方法之外的其它方法,仅在location下使用

method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH

句法:    limit_except method ... { ... }
默认:    -
内容:    location

案例

 
limit_except GET {
    allow 192.168.1.0/24;
    deny any;
}
#除了GET和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用
#method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH

文件操作优化的配置

aio

aio on | off | threads[=pool]; 是否启用aio功能,默认off

在FreeBSD和Linux上启用或禁用异步文件I/O (AIO):

 
Syntax:    aio on | off | threads[=pool];
Default:    aio off;
Context:    http, server, location
This directive appeared in version 0.8.11.

directio

当文件大于等于给定大小时,同步(直接)写磁盘,而非写缓存,默认off

Syntax:    directio size | off;
Default:    directio off;
Context:    http, server, location
This directive appeared in version 0.7.7.

sendfile

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用必须设为 on
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime。
#是否启用sendfile功能,在内核中封装报文直接发送,默认Off

Syntax:    sendfile on | off;
Default:    sendfile off;
Context:    http, server, location, if in location

案例

location /video/ {
  sendfile on;
  aio on;
  directio 8m;
}

open_file_cache

句法:    open_file_cache off;
         open_file_cache max=N [inactive=time];
默认:    open_file_cache关闭;
内容:    http,server,location

nginx可以缓存以下三种信息: (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间 (2) 打开的目录结构 (3) 没有找到的或者没有权限访问的文件的相关信息

max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现管理 inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除

open_file_cache_errors

是否缓存查找时发生错误的文件一类的信息,默认值为off

句法:    open_file_cache_errors on | off;
默认:    open_file_cache_errors关闭;
内容:    http,server,location

open_file_cache_min_uses number;

句法:    open_file_cache_min_uses number;
默认:    open_file_cache_min_uses 1;
内容:    http,server,location

open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1

设置*number*open_file_cache伪指令的inactive参数配置的时间段内文件访问的最少时间,这是文件描述符在高速缓存中保持打开状态所必需的。

open_file_cache_valid time;

句法:    open_file_cache_valid time;
默认:    open_file_cache_valid 60s;
内容:    http,server,location

缓存项有效性的检查频率,默认值为60s

 

 

标签:body,core,http,module,server,location,file,open
来源: https://www.cnblogs.com/yaokaka/p/13639657.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有