Nginx的几个重要模块

ngx_http_ssl_module

  让Nginx可以支持HTTPS的模块,此模块下的大多数指令都应用在http,server上下文

  ①ssl on | off;

   是否开启ssl功能

  ②ssl_certificate file;

   当前虚拟主机使用的PEM格式的证书文件

  ③ssl_certificate_key file;

   当前虚拟主机使用的证书中与公钥配对的私钥文件

  ④ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];

   ssl协议的版本,SSLv2不安全,建议使用TLS,默认使用的是TLS

  ⑤ssl_session_timeout time;

   ssl会话超时时长,指ssl会话中缓存条目的有效时长,默认为5m

  ⑥ssl_session_cache off | none | [builtin[:size]] [shared:name:size];

   指明ssl会话的缓存机制,默认是关闭的

builtin  //使用openssl内建的缓存机制,为worker独有
shared  //由各worker共享的缓存
         name //缓存空间的名称
         size  //缓存空间的大小/单位字节,每1MB内存空间可缓存4000个会话

  ⑦ssl_prefer_server_ciphers on | off;

   倾向于使用服务器端的加密算法,默认是关闭的

ngx_http_log_module

  此模块可以基于给定的格式记录请求于日志中

  ①access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

   access_log off;

   用在http,server,location,if in location,limit_except上下文

   默认值是access_log logs/access.log combined;

  ②log_format name string ...;

   定义日志格式

   用在http上下文中

  ③open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

   open_log_file_cache off;

   定义开启日志文件缓存功能

   用于http,server,location上下文,默认是关闭的

max //最大缓存条目
inactive=time //非活动时长,默认为10s
min_uses //最少使用次数,默认为1次
valid=time //验证缓存条目有效性的时长,默认为60s

ngx_http_rewrite_module

  基于此模块实现对请求的URL进行重写、重定向

  ①rewrite regex replacement [flag];

   用在server,location,if上下文中

/*①把用户请求的URI基于regex做检查,匹配到时将替换为replacement指定的字符串
②在同一个location中存在多个rewrite规则会自上而下逐个被检查(最多循环10次),可以使用flag控制此循环功能
③如果replacement中有以http或https开头,则替换结果会直接以重定向的方式返回客户端*/

[flag]:
last  //重写完成后停止对当前url在当前Location中的后续其他重写操作,改为对新url的新一轮处理;
break  //重写完成后停止对当前url在当前Location中的后续其他重写操作;(退出rewrite中的规则)
redirect  //重写完成后以临时重定向方式直接返回重写后生成的新的URL给客户端,由客户端对新的URL再次请求(响应码:302)
permanent  //重写完成后以永久重定向方式直接返回重写后生成的新的URL给客户端,由客户端对新的URL再次请求(响应码:301)

  ②rewrite_log on | off;

   是否启用重写日志;启用时,日志信息被发往错误日志,默认是关闭的

   用在http,server,location,if上下文

  ③if (condition) { ... }

   条件判断机制,在条件满足时,执行配置块中的配置,引入了一个新的配置上下文

   仅用在server,location上下文中

condition:
//比较表达式:
    ==,!=
    ~  //模式匹配,区分字符大小写
    ~*  //模式匹配,不区分字符大小写
    !~  //模式不匹配,区分字符大小写
    !~*  //模式不匹配,不区分字符大小写
//文件及目录存在性判断:
    -f,!-f  //文件
    -d,!-d //目录
    -e,!-e  //存在
    -x,!-x  //执行权限

//示例:
if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}
if ($request_method = POST) {
    return 405;
}
if ($slow) {
    limit_rate 10k;
}
if ($invalid_referer) {
    return 403;
}

  ④return code [text];

   return code URL;

   return URL;

   以指定的响应码和URL回应客户端

   用在server,location,if上下文中

  ⑤set $variable value;

   用户自定义变量

   用在server,location,if上下文中

ngx_http_gzip_module

  过滤器,对指定类型的资源压缩传输以节约带宽

  ①gzip on | off;

   启用或禁用gzip压缩响应报文

   用在http,server,location,if in location上下文中

  ②gzip_comp_level level;

   指定压缩比(1-9),默认为1

   用在http,server,location上下文

  ③gzip_disable regex ...;

   regex是匹配客户端浏览器类型的模式,表示对匹配到的浏览器不执行压缩响应报文

   用在http,server,location上下文

  ④gzip_min_length length;

   触发启用压缩功能的响应报文的最小长度

   用在http,server,location上下文

  ⑤gzip_http_version 1.0 | 1.1;

   设定启用压缩功能时,协议的最小版本,如果设置为1.1,1.0则不压缩

   用在http,server,location上下文

  ⑥gzip_types mime-type ...;

   指定仅执行压缩的资源内容类型,默认为text/html

   用在http,server,location上下文

  ⑦gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;

   对代理的请求基于何种属性判断其是否应该启用压缩功能

   用在http,server,location上下文

ngx_http_fastcgi_module

  基于此模块实现与php-fpm结合

  ①fastcgi_pass address;

   指明后端的服务器,address是fpm服务器监听的地址和端口

   用在location和if in location上下文

  ②fastcgi_index name;

   fastcgi应用的主页名称

   用在http,server,location上下文

  ③fastcgi_param parameter value [if_not_empty];

   传递给fpm服务器的参数及其值

   用在http,server,location上下文

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING    $query_string;
fastcgi_param REQUEST_METHOD  $request_method;
fastcgi_param CONTENT_TYPE    $content_type;
fastcgi_param CONTENT_LENGTH  $content_length;

//更多参数查阅fastcgi_params

  ④fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

   定义fastcgi的缓存相关,仅能定义在http上下文

path //文件系统路径,用于存储缓存的文件数据
max_size=size //定义此路径下的多大空间用于存储缓存数据
levels=#[:#[:#]] //缓存目录层级定义
    //如:levels=1:2
keys_zone=name:size //内存空间中用于缓存k/v映射关系的空间名称及大小
inactive=time //非活动时间

  ⑤fastcgi_cache zone | off;

   启用缓存功能,存于哪个zone中,依赖于上个配置中的定义

   用在http,server,location上下文

  ⑥fastcgi_cache_key string;

   定义要使用的缓存键

   用在http,server,location上下文  

//示例
fastcgi_cache_key localhost:9000$request_uri;

  ⑦fastcgi_cache_methods GET | HEAD | POST ...;

   缓存基于哪些请求方法的相关数据

   用在http,server,location上下文

  ⑧fastcgi_cache_min_uses number;

   指定时间内缓存的最少使用次数

   用在http,server,location上下文

  ⑨fastcgi_cache_valid [code ...] time;

   对不同响应码设定其可缓存时长

//示例
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404      1m;

  注意:启用缓存功能时,至少应该指定的三个参数:fastcgi_cache,fastcgi_cache_key,fastcgi_cache_valid

/*
Nginx缓存的实现机制

分两部分存储缓存信息,
一部分放于内存中:key-value形式
    其中key是url,value是缓存信息文件的校验码(md5)
另一部分是将缓存信息的文件置于磁盘上,并基于md5校验码实现分级存储,以便查找
*/

ngx_http_proxy_module

  基于此模块实现反代客户端请求至后端服务器

  ①proxy_pass URL;

   代理指令,后跟后端主机的URL

   用在location,if in location,limit_except上下文

//①proxy_pass后面的路径不带uri时,会把location的uri传递给后端主机
location /uri/ {
    proxy_pass http://HOST;
}
//假如location中的/uri/是/bbs/,访问的站点是www.a.com,则通过proxy_pass传递给后端主机的URL是:http://www.a.com/bbs/

//②proxy_pass后面路径是一个uri时,其会将location的uri替换为proxy_pass后端主机的uri
location /uri/ {
    proxy_pass http://HOST/new_uri/;
}
//假如location中的/uri/是/bbs/,访问的站点是www.a.com,proxy_pass后面的new_uri是/BBS/,则通过proxy_pass传递给后端主机的URL是:http://www.a.com/BBS/;

//③如果location定义uri时使用了正则表达式匹配机制,则proxy_pass后的路径必须不能使用uri
location ~|~* PATTERN {
    proxy_pass http://HOST;
}

  ②proxy_set_header field value;

   设定向后端主机发送的请求报文的首部及其值

   用在http,server,location上下文

proxy_set_header Real_Client $remote_addr(client addr);
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
//保留原来的值再附加新值(建议使用)

  ③proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

   定义缓存路径

   只能定义在http上下文

//示例
proxy_cache_path /var/cache levels=2:1 keys_zone=mycache:10m max_size=10m;

  ④proxy_cache zone | off;

   调用缓存,默认为off

   用在http,server,location上下文

  ⑤proxy_cache_key string;

   定义缓存键;

   用在http,server,location上下文

//示例
proxy_cache_key $request_uri(一般是请求uri)
proxy_cache_key $scheme$proxy_host$request_uri

  ⑥proxy_cache_valid [code ...] time;

   为不同的响应码设定其缓存的时长

   用在http,server,location上下文

//示例
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;

  ⑦proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

   向后端请求不到相应资源时基于哪种响应码或错误,使用过期缓存响应客户端

   用在http,server,location上下文

  ⑧proxy_connect_timeout time;

   与后端建立连接的超时时长,默认60s,最长为75s

   用在http,server,location上下文

  ⑨proxy_read_timeout time;

   等待后端主机发送响应报文的超时时长,默认为60s

   用在http,server,location上下文

  ⑨+①proxy_send_timeout time;

   向后端服务器发送请求报文的超时时长,默认为60s

   用在http,server,location上下文

ngx_http_headers_module

  基于此模块在响应给客户端的报文中添加首部

  ①add_header name value [always];

   向响应报文添加自定义首部,并赋值

   用在http,server,location,if in location上下文  

add_header X-Cache $upstream_cache_status;

//可查看是否命中缓存
//示例:
add_header x-via $server_addr(接收请求报文的服务器地址)

  ②expires [modified] time;

   expires epoch | max | off;

   用于添加Expire及Cache-Control首部或修改其值

   用在http,server,location,if in location上下文

ngx_http_upstream_module

  基于此模块实现nginx的负载均衡:定义服务器组,将多个后端主机定义为服务器组,而后可由proxy_pass,fastcgi_pass,memcached_pass等调用

  ①upstream name { ... };

   定义后端服务器组,引入新的上下文,只能用于http上下文

  ②server address [parameters];

   定义后端服务器的地址和相关参数

   仅用于在upstream上下文

地址格式:
    IP[:port]
    HOSTNAME[:port]
    unix:/PATH/TO/SOME_SOCK_FILE
参数:
    weight=number //服务器调度权重
    max_fails=number //最大失败尝试次数
    fail_timeout=time //设置服务器失败(不可用)的超时时长
    backup //备用主机,相当于sorry_server
    down //手动标记其不再处理任何用户请求

  ③ip_hash;

   指定调度算法:源地址哈希

   仅用于在upstream上下文

  ④least_conn;

   指定调度算法:最少连接

   仅用于在upstream上下文

  ⑤keepalive connections;

   保持连接的个数

   仅用于在upstream上下文

  ⑥health_check [parameters];

   定义后端主机的健康状态检测

   只能在location中使用

//可用参数:
    interval=# //检测的频度,默认为5s
    fails=# //判定为失败的检测次数
    passes=# //判定为成功的检测次数
    uri=uri //执行检测时请求的uri,默认为主页
    match=name //基于那个match做检测结果为'ok'或'not ok'的判定
    port=# //向服务器的那个端口发起检测请求

  ⑦match name {...};

   对后端主机做检测时,定义其结果的判断标准,nginx2.0+才支持

   仅用于http上下文

//专用指令:
status:期望的响应码
    status CODE
    status !CODE
    status CODE-CODE
header:基于响应首部进行判断
    header HEADER=VALUE
    header HEADER!=VALUE
    header [!]HEADER
    header HEADER~VALUE
body:期望的响应报文的主体部分该有的内容
    body ~"CONTENT"
    body !~"CONTENT"

  ⑧hash key [consistent];

   定义调度方法,可自定义基于何种信息(key)进行绑定

   仅用于在upstream上下文

hash $remote_addr
hash $request_uri
hash $cookie_username

 

posted on 2017-02-16 20:45  林纳克斯  阅读(6354)  评论(0编辑  收藏  举报

导航