nginx查询stream插件 查看nginx模块_nginx查询stream插件

nginx的模块

nginx高度模块化,但其模块早期不支持DSO机制;1.9.11版本支持动态装载和卸载

核心模块:core module

标准模块:

  • HTTP 模块:ngx_http_*
  • Mail 模块 ngx_mail_*
  • Stream 模块 ngx_stream_*

第三方模块:

ngx_http_core_module 核心模块

1)server { ... }:配置虚拟主机

# vim /etc/nginx/nginx.conf
 http  { 
    server {
        listen 80;
        server_name www.miaosen.tech;
        root /data/www/;
    }
    server {
        listen 80;
        server_name news.miaosen.tech;
        root /data/news/;
    }
}
# nginx  启动服务
# echo news site > /data/news/index.html
# echo www site > /data/www/index.html


  

客户端测试:



# curl www.miaosen.tech
www site
# curl news.miaosen.tech
news site



  

2)listen port|address[:port]|unix:/PATH/TO/SOCKET_FILE
 
 
listen address[:port] [default_server] [ssl] [http2 | spdy]  [backlog=number] [rcvbuf=size] [sndbuf=size]



  

  • default_server:设定为默认虚拟主机
  • ssl:限制仅能够通过ssl连接提供服务
  • backlog=number:超过并发连接数后,新请求进入后援队列的长
  • rcvbuf=size:接收缓冲区大小
  • sndbuf=size:发送缓冲区大小

listen PORT; 指令监听在不同的端口,可实现基于端口的虚拟主机

listen IP:PORT; 监听 IP 地址不同,实现基于IP的虚拟主机

3)server_name name ...

虚拟主机的主机名称后可跟多个由空白字符分隔的字符串

支持*通配任意长度的任意字符



server {
        listen 80;
        server_name *.miaosen.tech;
        root /data/default/;
    }
# echo default > /data/default/index.html
# nginx -s reload
# curl xxx.miaosen.tech
default



  

支持 ~ 起始的字符做正则表达式模式匹配,性能原因慎用


server_name     ~^www\d+\.miaosen\.tech$  #说明:\d 表示 [0-9]


  

匹配优先级机制从高到低:

(1) 首先是字符串精确匹配 如:www.miaosen.com

(2) 左侧*通配符 如:*.miaosen.tech

(3) 右侧*通配符 如:www.miaosen.*

(4) 正则表达式 如: ~^.*\.miaosen\.tech$

(5) default_server

4)tcp_nodelay on|off

在keepalived模式下的连接是否启用TCP_NODELAY选项

当为off时,延迟发送,合并多个请求后再发送

默认on时,不延迟发送

可用于:http, server, location

如果为了节约服务器性能可以打开,如果为了用户体验更好选择关闭

5)sendfile on|off

是否启用sendfile功能,在内核中封装报文直接发送,默认关闭

6)server_tokens on|off|build|string

是否在响应报文的Server首部显示nginx版本,建议关闭

7)root

设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location

8)location [ = | ~ | ~* | ^~ ] uri { ... }

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置



# mkdir /data/www/blog/
# echo blog > /data/www/blog/index.html
# vim /etc/nginx/nginx.conf
    server {
        listen 80;
        server_name www.miaosen.tech;
        root /data/www/;
        location /blog {
            root /data/www/;
        }
    }
# curl http://www.miaosen.tech/blog/  #测试
blog



  

=:对URI做精确匹配

^~: 对URI的最左边部分做匹配检查,不区分字符大小写

~: 对URI做正则表达式模式匹配,区分字符大小写

~*: 对URI做正则表达式模式匹配,不区分字符大小写

不带符号:匹配起始于此uri的所有的uri

匹配优先级从高到低:=, ^~, ~/~*, 不带符号

例如:


location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}


  



The “/” request will match configuration A, 
the “/index.html” request will match configuration B, 
the “/documents/document.html” request will match configuration C, 
the “/images/1.gif” request will match configuration D, 
and the “/documents/1.jpg” request will match configuration E.



  

9)alias

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


server {
        listen 80;
        server_name www.miaosen.tech;
        root /data/www/;
        location /blog;
            alias /data/www/blog;  #和root /data/www/;作用相同
        }
    }
# curl http://www.miaosen.tech/blog/
blog


  

注意:location中使用root指令和alias指令的意义不同

(a) root,给定的路径对应于location中的/uri/左侧的/

(b) alias,给定的路径对应于location中的/uri/右侧的/

10)index file ...

指定默认网页文件,注意需要装载 ngx_http_index_module 模块

11)error_page code ... [=[response]] uri

定义错误页,以指定的响应状态码进行响应;可用位置:http, server, location, if in location


# echo "404 not found page" > /data/www/404.html
    server {
        listen 80;
        server_name www.miaosen.tech;
        root /data/www/;
        error_page 404 /404.html;
    }
# curl http://www.miaosen.tech/notfound.html
404 not found page
    server {
        listen 80;
        server_name www.miaosen.tech;
        root /data/www/;
        error_page 404 =200 /404.html;  #将404返回码重定向成200访问码,防止浏览器劫持
    }
# curl -I http://www.miaosen.tech/notfound.html
HTTP/1.1 200 OK  #测试为200正确访问码


  

12)

try_files file ... uri | =code

按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误


# echo default page > /data/news/default.html
    server {
        listen 80;
        server_name news.miaosen.tech;
        root /data/news/;
        location / {
            try_files $uri /default.html;  #如果用户访问的URI不存在则放回默认页面
        }
    }
# curl http://news.miaosen.tech/index.html   
news site
# curl http://news.miaosen.tech/noindex.html
default page
    server {
        listen 80;
        server_name news.miaosen.tech;
        root /data/news/;
        location / {
            try_files $uri $uri/index.html $uri.html =404;
        }
    }
# curl http://news.miaosen.tech/index.html  
news site
# curl http://news.miaosen.tech/noindex.html
404 Not Found



  

13)keepalive_timeout timeout [header_timeout]

设定保持连接超时时长,0表示禁止长连接,默认为75s,可用于http, server, location

14)keepalive_requests number

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

15)keepalive_disable none | browser ...

对哪类型的浏览器禁用长连接

16)send_timeout time

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

17)client_body_buffer_size size

用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;

超出此大小时,其将被暂存到磁盘上的由下面 client_body_temp_path 指令所定义的位置

18)client_body_temp_path path [level1 [level2 [level3]]]

设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量

19)limit_rate rate

限制响应给客户端的传输速率,单位是bytes/second;默认值0表示无限制

20)limit_except method ... { ... }

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

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



location /upload {
    root /date/www/;
    limit_except GAT {
        allow 192.168.0.9/24;
        deny all;
    }
}
# 除了 GET和HEAD 之外其它方法仅允许192.168.0.9/24主机使用



  

21)aio on | off | threads[=pool]

是否启用aio功能

22)directio size | off

当文件大于等于给定大小时,例如directio 4m,同步到磁盘,而非写缓存,以防数据丢失

23)open_file_cache off | max=N [inactive=time]

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

24)open_file_cache_errors on | off

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

25)open_file_cache_min_uses number

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

26)open_file_cache_valid time

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