本篇文章主要讲 ngx_stream_core_module 模块下各指令的使用方法,Nginx默认未配置该模块,需要用“--with-stream”配置参数重新编译Nginx。
worker_processes auto;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
stream {
upstream backend {
hash $remote_addr consistent;
server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
upstream dns {
server 192.168.0.1:53535;
server dns.example.com:53;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
server {
listen 127.0.0.1:53 udp reuseport;
proxy_timeout 20s;
proxy_pass dns;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
}
1. stream
Syntax: stream { ... }
Default: —
Context: main
为指定流服务器的指令提供配置文件上下文。
2. server
Syntax: server { ... }
Default: —
Context: stream
设置服务器配置。
3. listen
Syntax: listen address:port [ssl] [udp] [proxy_protocol] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default: —
Context: server
设置服务器接受连接的套接字address和port。可以仅指定port。address也可以是主机名。可以监听一个端口段range。
listen 127.0.0.1:12345;
listen *:12345;
listen 12345; # same as *:12345
listen localhost:12345;
# IPv6 地址用方括号指定:
listen [::1]:12345;
listen [::]:12345;
# UNIX 域套接字以 "unix: "前缀指定:
listen unix:/var/run/nginx.sock;
# 端口范围用连字符分隔第一个和最后一个端口:
listen 127.0.0.1:12345-12399;
listen 12345-12399;
udp:指定一个用于处理数据包的监听套接字(1.9.13),是否启用UDP协议支持。为了在同一会话中处理来自同一地址和端口的数据包,还应指定 reuseport 参数。
4. proxy_protocol_timeout 代理协议超时时间
Syntax: proxy_protocol_timeout timeout;
Default: proxy_protocol_timeout 30s;
Context: stream, server
This directive appeared in version 1.11.4.
指定读取代理协议头的超时时间。如果在这段时间内没有传输完整个报头,连接将被关闭。
5. preread_buffer_size 预读取缓冲大小
Syntax: preread_buffer_size size;
Default: preread_buffer_size 16k;
Context: stream, server
This directive appeared in version 1.11.5.
指定预读取缓冲区的大小。
6. preread_timeout 预读取超时时间
Syntax: preread_timeout timeout;
Default: preread_timeout 30s;
Context: stream, server
This directive appeared in version 1.11.5.
指定预读取阶段的超时时间。
7. resolver DNS服务器
Syntax: resolver address ... [valid=time] [ipv4=on|off] [ipv6=on|off] [status_zone=zone];
Default: —
Context: stream, server
This directive appeared in version 1.11.3.
配置用于解析上游服务器域名的DNS服务器,
resolver 127.0.0.1 [::1]:5353;
resolver 127.0.0.1 [::1]:5353 valid=30s;
8. resolver_timeout 解析超时时间
Syntax: resolver_timeout time;
Default: resolver_timeout 30s;
Context: stream, server
This directive appeared in version 1.11.3.
设置解析超时时间。
resolver_timeout 5s;
9. tcp_nodelay
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: stream, server
This directive appeared in version 1.9.4.
启用或禁用 TCP_NODELAY 选项。客户端和代理服务器连接均可启用该选项。
10. variables_hash_bucket_size
Syntax: variables_hash_bucket_size size;
Default: variables_hash_bucket_size 64;
Context: stream
This directive appeared in version 1.11.2.
设置变量哈希表的桶大小。
11. variables_hash_max_size
Syntax: variables_hash_max_size size;
Default: variables_hash_max_size 1024;
Context: stream
This directive appeared in version 1.11.2.
设置变量哈希表的最大大小。