Nginx 配置文件简介-5(四层负载均衡)
ngx_stream_core_module
基于四层的负载均衡
指令详解:
listen
语法:listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
默认值:无
应用位置:server
作用:设置服务器将接受连接的套接字的地址和端口。 可以仅指定端口。 地址也可以是主机名。不同的服务器必须侦听不同的端口地址。
preread_buffer_size
语法:preread_buffer_size size;
默认值:preread_buffer_size 16k;
应用位置:stream, server
作用:指定预读缓冲区的大小。
preread_timeout
语法:preread_timeout timeout;
默认值:preread_timeout 30s;
应用位置:stream, server
作用:指定预读阶段的超时。
proxy_protocol_timeout
语法:proxy_protocol_timeout timeout;
默认值:proxy_protocol_timeout 30s;
应用位置:stream, server
作用:指定一个超时,以读取要完成的代理协议标头。如果在此期间没有传输整个标头,则连接关闭。
resolver
语法:resolver address ... [valid=time] [ipv6=on|off];
默认值:无
应用位置:stream, server
作用:配置用于将上游服务器的名称解析为地址的名称服务器
resolver_timeout
语法:resolver_timeout time;
默认值:resolver_timeout 30s;
应用位置:stream, server
作用:设置名称解析超时
server
语法:server { ... }
默认值:无
应用位置:stream
作用:设置服务器的配置。
stream
语法:stream { ... }
默认值:无
应用位置:main
作用:提供指定流服务器指令的配置文件上下文。
tcp_nodelay
语法:tcp_nodelay on | off;
默认值:tcp_nodelay on;
应用位置:stream, server
作用:启用或禁用TCP_NODELAY选项。该选项对客户机和代理服务器连接都启用。
variables_hash_bucket_size
语法:variables_hash_bucket_size size;
默认值:variables_hash_bucket_size 64;
应用位置:stream
作用:设置变量哈希表的桶大小
variables_hash_max_size
语法:variables_hash_max_size size;
默认值:variables_hash_max_size 1024;
应用位置:stream
作用:设置变量哈希表的最大大小。
ngx_stream_access_module
ngx_stream_access_module模块(1.9.2)允许限制对某些客户端地址的访问。
比较简单,仅仅列出案例
server {
...
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
ngx_stream_geo_module
ngx_stream_geo_module模块(1.11.3)使用取决于客户端IP地址的值创建变量。类似 ngx_http_geo_module. 仅给个案例.
geo $geo {
default 0;
127.0.0.1 2;
192.168.1.0/24 1;
10.1.0.0/16 1;
::1 2;
2001:0db8::/32 1;
}
ngx_stream_geoip_module
ngx_stream_geoip_module模块(1.11.3)使用预编译的MaxMind数据库创建具有取决于客户端IP地址的值的变量。类似于 ngx_http_geoip_module.仅仅给个案例。
stream {
geoip_country GeoIP.dat;
geoip_city GeoLiteCity.dat;
map $geoip_city_continent_code $nearest_server {
default example.com;
EU eu.example.com;
NA na.example.com;
AS as.example.com;
}
...
}
ngx_stream_limit_conn_module
ngx_stream_limit_conn_module模块(1.9.3)用于限制每个定义密钥的连接数,特别是来自单个IP地址的连接数。
指令详解:
limit_conn
语法:limit_conn zone number;
默认值:无
应用位置:stream, server
作用:设置共享内存区域和给定键值的最大允许连接数。超过此限制后,服务器将关闭连接
limit_conn_log_level
语法:limit_conn_log_level info | notice | warn | error;
默认值:limit_conn_log_level error;
应用位置:stream, server
作用:设置服务器限制连接数量时所需的日志级别。
limit_conn_zone
语法:limit_conn_zone key zone=name:size;
默认值:无
应用位置:stream
作用:为各种键保持状态的共享内存区域设置参数。特别是,状态包括当前连接的数量。键可以包含文本、变量及其组合(1.11.2)。
案例:
stream {
limit_conn_zone $binary_remote_addr zone=addr:10m;
...
server {
...
limit_conn addr 1;
limit_conn_log_level error;
}
}
ngx_stream_log_module
ngx_stream_log_module模块(1.11.4)以指定的格式写入会话日志。
指令详解:
access_log
语法:access_log path format [buffer=size] [gzip[=] [level]flush=time] [if=condition]; access_log off;
默认值:access_log off;
应用位置:stream, server
作用:设置缓冲日志写入的路径、格式和配置。可以在同一级别指定多个日志。可以通过在第一个参数中指定“syslog:”前缀来配置对syslog的日志记录。特殊值off取消了当前级别上的所有access_log指令。
log_format
语法:log_format name [escape=default|json|none] string ...;
默认值:无
应用位置:stream
作用:指定日志格式。
open_log_file_cache
语法:open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off;
默认值:open_log_file_cache off;
应用位置:stream, server
作用:定义一个缓存,用于存储名称包含变量的常用日志的文件描述符。场景是多个日志文件时使用. 类似 ngx_http_log_module 模块.
案例:
log_format basic '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time';
access_log /spool/logs/nginx-access.log basic buffer=32k;
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
ngx_stream_map_module
ngx_stream_map_module模块(1.11.2)创建的变量的值依赖于其他变量的值。类似于 ngx_http_map_module 模块,应用位置 为 stream, 仅仅给个案例:
map $remote_addr $limit {
127.0.0.1 "";
default $binary_remote_addr;
}
limit_conn_zone $limit zone=addr:10m;
limit_conn addr 1;
ngx_stream_proxy_module
ngx_stream_proxy_module模块(1.9.0)允许通过TCP、UDP(1.9.13)和unix域套接字代理数据流。好多参数类似于 ngx_http_proxy_module 模块,应用位置不用而已,可以参考前面的模块详解。仅给出案例:
server {
listen 127.0.0.1:12345;
proxy_pass 127.0.0.1:8080;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 1m;
proxy_pass example.com:12345;
}
server {
listen 53 udp reuseport;
proxy_timeout 20s;
proxy_pass dns.example.com:53;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
ngx_stream_realip_module
ngx_stream_realip_module模块用于将客户机地址和端口更改为在代理协议头(1.11.4)中发送的地址。代理协议之前必须通过在listen指令中设置proxy_protocol参数来启用。
指令详解:
set_real_ip_from
语法:set_real_ip_from address | CIDR | unix:
默认值:无
应用位置:stream, server
作用:定义已知可发送正确替换地址的可信地址。 如果指定了特殊值unix:,则所有UNIX域套接字都将受信任。
案例:
listen 12345 proxy_protocol; set_real_ip_from 192.168.1.0/24; set_real_ip_from 192.168.2.1;
ngx_stream_return_module
ngx_stream_return_module模块(1.11.2)允许向客户端发送指定值,然后关闭连接。
语法:return value;
默认值:无
应用位置:server
作用:指定要发送到客户端的值。 该值可以包含文本,变量及其组合。
案例:
server {
listen 12345;
return $time_iso8601;
}
ngx_stream_split_clients_module
ngx_stream_split_clients_module模块(1.11.3)创建适用于A / B测试的变量,也称为拆分测试。
语法:split_clients string $variable { ... }
默认值:无
应用位置:stream
作用:为A / B测试创建变量
案例:
stream {
...
split_clients "${remote_addr}AAA" $upstream {
0.5% feature_test1;
2.0% feature_test2;
* production;
}
server {
...
proxy_pass $upstream;
}
}
使用MurmurHash2对原始字符串的值进行哈希处理。 在给出的示例中,从0到21474835(0.5%)的哈希值对应于$ variant变量的值“.one”,从21474836到107374180(2%)的哈希值对应于值“.two”和哈希值 从107374181到4294967295的值对应于值“”(空字符串)。
ngx_stream_ssl_module
ngx_stream_ssl_module模块(1.9.0)为流代理服务器提供必要的支持,以使用SSL / TLS协议。 默认情况下不构建此模块,应使用--with-stream_ssl_module配置参数启用它。
配置参数大多同以前的参数,仅仅给个案例:
stream {
...
server {
listen 12345 ssl;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /usr/local/nginx/conf/cert.pem;
ssl_certificate_key /usr/local/nginx/conf/cert.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
...
}
ngx_stream_upstream_module
ngx_stream_upstream_module模块(1.9.0)用于定义可由proxy_pass指令引用的服务器组。类似于 ngx_http_upstream_module 模块,仅出个案例.
upstream backend {
hash $remote_addr consistent;
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345;
server unix:/tmp/backend3;
server backup1.example.com:12345 backup;
server backup2.example.com:12345 backup;
}
server {
listen 12346;
proxy_pass backend;
}
ngx_stream_upstream_hc_module
ngx_stream_upstream_hc_module模块(1.9.0)允许对组中的服务器启用定期运行状况检查。 服务器组必须驻留在共享内存中。商业版本可用。仅仅给个案例.
upstream tcp {
zone upstream_tcp 64k;
server backend1.example.com:12345 weight=5;
server backend2.example.com:12345 fail_timeout=5s slow_start=30s;
server 192.0.2.1:12345 max_fails=3;
server backup1.example.com:12345 backup;
server backup2.example.com:12345 backup;
}
server {
listen 12346;
proxy_pass tcp;
health_check;
}
ngx_stream_zone_sync_module
ngx_stream_zone_sync_module模块(1.13.8)为在集群节点之间同步共享内存区域的内容提供了必要的支持。要为特定区域启用同步,对应的模块必须支持此特性。目前,可以在HTTP和stream中同步HTTP粘性会话、关于过量HTTP请求的信息和键值对。 商业版本才有.
ngx_google_perftools_module
ngx_google_perftoos_module模块(0.6.29)支持使用Google Performance Tools分析nginx工作进程。 该模块适用于nginx开发人员。
该模块需要gperftools库。
语法:google_perftools_profiles file;
默认值:无
应用位置:main
作用:设置一个文件名,用于保存nginx工作进程的分析信息。 工作进程的ID始终是文件名的一部分,并在点后面附加到文件名的末尾。
案例:
google_perftools_profiles /path/to/profile; # 配置文件将存储为/ path / to / profile。<worker_pid>。

浙公网安备 33010602011771号