非常教程

Nginx参考手册

ngx_stream_core_module

ngx_stream_core_module

  • 示例配置
  • 指令
  • preread_buffer_size
  • preread_timeout
  • proxy_protocol_timeout
  • 解决
  • resolver_timeout
  • 服务器
  • tcp_nodelay
  • variables_hash_bucket_size
  • variables_hash_max_size
  • 嵌入式变量

ngx_stream_core_module模块自1.9.0版开始可用。该模块不是默认生成的,它应该使用--with-stream配置参数启用。

示例配置

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;
        proxy_responses 1;
        proxy_timeout 20s;
        proxy_pass dns;
    }

    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
}

指令

句法:

监听地址:port ssl proxy_protocol rcvbuf = size bind reuseportso_keepalive = on | off | keepidle:keepintvl:keepcnt];

默认:

语境:

服务器

设置address以及port对哪些服务器将接受连接插座。可以只指定端口。地址也可以是主机名,例如:

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;

ssl参数允许指定此端口上接受的所有连接都应该在SSL模式下工作。

udp参数配置了一个用于处理数据报的监听套接字(1.9.13)。

proxy_protocol参数(1.11.4)允许指定这个端口上接受的所有连接应使用代理服务器协议。

listen指令可以具有特定于套接字相关系统调用的几个附加参数。

backlog= 在限制未决连接队列(1.9.2)队列的最大长度的调用中number设置backlog参数listen()。默认情况下,backlog在FreeBSD,DragonFly BSD和macOS上设置为-1,在其他平台上设置为511。rcvbuf= size设置SO_RCVBUF侦听套接字(1.11.13)的接收缓冲区大小(选项)。sndbuf= size设置SO_SNDBUF侦听套接字(1.11.13)的发送缓冲区大小(选项)。bind此参数指示bind()为给定地址:端口对进行单独调用。事实是,如果有几个listen指令具有相同的端口但地址不同,并且其中一个listen指令侦听给定端口的所有地址(*:port),nginx将bind()只会*:port。应该注意的getsockname()是,在这种情况下系统调用将确定接受连接的地址。如果使用ipv6onlyso_keepalive参数,那么对于给定的addressportbind()将始终进行单独的呼叫。ipv6only= on| off此参数确定(通过IPV6_V6ONLY套接字选项)侦听通配符地址的IPv6套接字是[::]仅接受IPv6连接还是接受IPv6和IPv4连接。该参数默认打开。它只能在启动时设置一次。reuseport这个参数(1.9.1)指示为每个工作进程创建一个单独的监听套接字(使用SO_REUSEPORT套接字选项),允许内核在工作进程之间分配传入连接。目前这只适用于Linux 3.9+和DragonFly BSD。

不恰当地使用此选项可能会产生其安全隐患。

so_keepalive= on| off| keepidlekeepintvlkeepcnt 此参数配置为监听套接字的“TCP保活”的行为。如果省略此参数,则操作系统的设置将对套接字有效。如果它被设置为值“ on”,则SO_KEEPALIVE该插座的选项被打开。如果它设置为值“ off”,SO_KEEPALIVE则关闭套接字选项。在每个插槽的基础使用的TCP保活参数某些操作系统支持设置TCP_KEEPIDLETCP_KEEPINTVLTCP_KEEPCNT套接字选项。在这样的系统(目前,Linux的2.4 +,NetBSD的5+和FreeBSD 9.0-STABLE),它们可以使用配置的keepidlekeepintvlkeepcnt参数。一个或两个参数可以省略,在这种情况下,相应套接字选项的系统默认设置将生效。例如,

so_keepalive=30m::10

会将空闲超时(TCP_KEEPIDLE)设置为30分钟,将探测间隔(TCP_KEEPINTVL)保留在其系统默认值下,并将探测计数(TCP_KEEPCNT)设置为10个探测。

不同的服务器必须监听不同的addressport对。

句法:

preread_buffer_size大小;

默认:

preread_buffer_size 16k;

语境:

流,服务器

指定一个size预读缓冲区。

句法:

preread_timeout超时;

默认:

preread_timeout 30s;

语境:

流,服务器

指定timeout预读取阶段。

句法:

proxy_protocol_timeout超时;

默认:

proxy_protocol_timeout 30s;

语境:

流,服务器

指定timeout用于读取PROXY协议头以完成。如果在这段时间内没有发送完整的报头,则连接关闭。

句法:

解析器地址...有效=时间;

默认:

语境:

流,服务器

将用于解析上游服务器名称的名称服务器配置为地址,例如:

resolver 127.0.0.1 [::1]:5353;

地址可以指定为域名或IP地址,以及可选端口。如果未指定端口,则使用端口53。名称服务器以循环方式查询。

默认情况下,nginx会在解析时查找IPv4和IPv6地址。如果不想查找IPv6地址,则ipv6=off可以指定参数。

默认情况下,nginx使用响应的TTL值缓存答案。可选valid参数允许覆盖它:

resolver 127.0.0.1 [::1]:5353 valid=30s;

在版本1.11.3之前,该指令作为我们的商业订阅的一部分提供。

句法:

resolver_timeout时间;

默认:

resolver_timeout 30s;

语境:

流,服务器

设置名称解析超时,例如:

resolver_timeout 5s;

在版本1.11.3之前,该指令作为我们的商业订阅的一部分提供。

句法:

服务器{...}

默认:

语境:

设置服务器的配置。

句法:

流{...}

默认:

语境:

主要

提供指定流服务器指令的配置文件上下文。

句法:

tcp_nodelay on | off;

默认:

tcp_nodelay on;

语境:

流,服务器

启用或禁用该TCP_NODELAY选项的使用。该选项对客户端和代理服务器连接均启用。

句法:

variables_hash_bucket_size大小;

默认:

variables_hash_bucket_size 64;

语境:

设置变量哈希表的桶大小。设置散列表的细节在单独的文档中提供。

句法:

variables_hash_max_size大小;

默认:

variables_hash_max_size 1024;

语境:

设置size变量哈希表的最大值。设置散列表的细节在单独的文档中提供。

嵌入式变量

ngx_stream_core_module模块自1.11.2开始支持变量。

$binary_remote_addr客户端地址以二进制形式表示,值的长度始终为IPv4地址的4个字节或IPv6地址的16个$bytes_received字节从客户端接收的字节数(1.11.4)$bytes_sent发送到客户端$connection连接的字节数序列号$hostname主机名$msec当前时间(以秒为单位)以毫秒为单位解析用于与客户端通信的工作进程协议的$nginx_versionnginx版本$pidPID $protocolTCPUDP(1.11.4)$proxy_protocol_addr来自PROXY协议头的客户端地址,否则为空字符串(1.11.4)

通过proxy_protocol在listen指令中设置参数,必须先启用PROXY协议。

$proxy_protocol_port 来自PROXY协议头的客户端端口,否则为空字符串(1.11.4)

通过proxy_protocol在listen指令中设置参数,必须先启用PROXY协议。

$remote_addr客户端地址$remote_port客户端端口$server_addr接受连接的服务器的地址

计算此变量的值通常需要一次系统调用。为了避免系统调用,listen指令必须指定地址并使用bind参数。

$server_port$session_time以毫秒为单位的分辨率(1.11.4)以秒为单位接受连接会话持续时间的服务器的端口; $status会话状态(1.11.4),可以是下列之一:200会话成功完成400客户端数据不能被解析,例如,代理协议报头403接入禁止的,例如,当访问被限制为某些客户端的地址500内部服务器错误502坏网关,例如,如果无法选择或到达上游服务器。503服务不可用,例如,当访问受限于$time_iso8601本地时间的本地时间以ISO 8601标准格式的$time_local本地时间的通用日志格式

ngx_stream_core_module
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module