`

NGINX tcp 反向代理

 
阅读更多

https://nginx.org/en/docs/stream/ngx_stream_core_module.html

Module ngx_stream_core_module

Example Configuration
Directives
     listen
     preread_buffer_size
     preread_timeout
     proxy_protocol_timeout
     resolver
     resolver_timeout
     server
     stream
     tcp_nodelay
     variables_hash_bucket_size
     variables_hash_max_size
Embedded Variables

The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

Example Configuration

 

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;
    }
}

 

Directives

Syntax: Default: Context:
listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
server

Sets the address and port for the socket on which the server will accept connections. It is possible to specify just the port. The address can also be a hostname, for example:

listen 127.0.0.1:12345;
listen *:12345;
listen 12345;     # same as *:12345
listen localhost:12345;

IPv6 addresses are specified in square brackets:

listen [::1]:12345;
listen [::]:12345;

UNIX-domain sockets are specified with the “unix:” prefix:

listen unix:/var/run/nginx.sock;

 

The ssl parameter allows specifying that all connections accepted on this port should work in SSL mode.

The udp parameter configures a listening socket for working with datagrams (1.9.13).

The proxy_protocol parameter (1.11.4) allows specifying that all connections accepted on this port should use the PROXY protocol.

The listen directive can have several additional parameters specific to socket-related system calls.

backlog=number
sets the backlog parameter in the listen() call that limits the maximum length for the queue of pending connections (1.9.2). By default, backlog is set to -1 on FreeBSD, DragonFly BSD, and Mac OS X, and to 511 on other platforms.
bind
this parameter instructs to make a separate bind() call for a given address:port pair. The fact is that if there are several listen directives with the same port but different addresses, and one of the listen directives listens on all addresses for the given port (*:port), nginx will bind() only to *:port. It should be noted that the getsockname() system call will be made in this case to determine the address that accepted the connection. If the ipv6only or so_keepalive parameters are used then for a given address:port pair a separate bind() call will always be made.
ipv6only=on|off
this parameter determines (via the IPV6_V6ONLY socket option) whether an IPv6 socket listening on a wildcard address [::] will accept only IPv6 connections or both IPv6 and IPv4 connections. This parameter is turned on by default. It can only be set once on start.
reuseport
this parameter (1.9.1) instructs to create an individual listening socket for each worker process (using the SO_REUSEPORT socket option), allowing a kernel to distribute incoming connections between worker processes. This currently works only on Linux 3.9+ and DragonFly BSD.
Inappropriate use of this option may have its security implications.
so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
this parameter configures the “TCP keepalive” behavior for the listening socket. If this parameter is omitted then the operating system’s settings will be in effect for the socket. If it is set to the value “on”, the SO_KEEPALIVE option is turned on for the socket. If it is set to the value “off”, the SO_KEEPALIVE option is turned off for the socket. Some operating systems support setting of TCP keepalive parameters on a per-socket basis using the TCP_KEEPIDLE, TCP_KEEPINTVL, and TCP_KEEPCNT socket options. On such systems (currently, Linux 2.4+, NetBSD 5+, and FreeBSD 9.0-STABLE), they can be configured using the keepidle, keepintvl, and keepcnt parameters. One or two parameters may be omitted, in which case the system default setting for the corresponding socket option will be in effect. For example,
so_keepalive=30m::10
will set the idle timeout (TCP_KEEPIDLE) to 30 minutes, leave the probe interval (TCP_KEEPINTVL) at its system default, and set the probes count (TCP_KEEPCNT) to 10 probes.

 

Different servers must listen on different address:port pairs.

Syntax: Default: Context:
preread_buffer_size size;
preread_buffer_size 16k;
stream, server

This directive appeared in version 1.11.5.

Specifies a size of the preread buffer.

Syntax: Default: Context:
preread_timeout timeout;
preread_timeout 30s;
stream, server

This directive appeared in version 1.11.5.

Specifies a timeout of the preread phase.

Syntax: Default: Context:
proxy_protocol_timeout timeout;
proxy_protocol_timeout 30s;
stream, server

This directive appeared in version 1.11.4.

Specifies a timeout for reading the PROXY protocol header to complete. If no entire header is transmitted within this time, the connection is closed.

Syntax: Default: Context:
resolver address ... [valid=time] [ipv6=on|off];
stream, server

This directive appeared in version 1.11.3.

Configures name servers used to resolve names of upstream servers into addresses, for example:

resolver 127.0.0.1 [::1]:5353;

An address can be specified as a domain name or IP address, and an optional port. If port is not specified, the port 53 is used. Name servers are queried in a round-robin fashion.

By default, nginx will look up both IPv4 and IPv6 addresses while resolving. If looking up of IPv6 addresses is not desired, the ipv6=off parameter can be specified.

By default, nginx caches answers using the TTL value of a response. The optional valid parameter allows overriding it:

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

 

Before version 1.11.3, this directive was available as part of our commercial subscription.

 

Syntax: Default: Context:
resolver_timeout time;
resolver_timeout 30s;
stream, server

This directive appeared in version 1.11.3.

Sets a timeout for name resolution, for example:

resolver_timeout 5s;

 

Before version 1.11.3, this directive was available as part of our commercial subscription.

 

Syntax: Default: Context:
server { ... }
stream

Sets the configuration for a server.

Syntax: Default: Context:
stream { ... }
main

Provides the configuration file context in which the stream server directives are specified.

Syntax: Default: Context:
tcp_nodelay on | off;
tcp_nodelay on;
stream, server

This directive appeared in version 1.9.4.

Enables or disables the use of the TCP_NODELAY option. The option is enabled for both client and proxied server connections.

Syntax: Default: Context:
variables_hash_bucket_size size;
variables_hash_bucket_size 64;
stream

This directive appeared in version 1.11.2.

Sets the bucket size for the variables hash table. The details of setting up hash tables are provided in a separate document.

Syntax: Default: Context:
variables_hash_max_size size;
variables_hash_max_size 1024;
stream

This directive appeared in version 1.11.2.

Sets the maximum size of the variables hash table. The details of setting up hash tables are provided in a separate document.

Embedded Variables

The ngx_stream_core_module module supports variables since 1.11.2.

$binary_remote_addr
client address in a binary form, value’s length is always 4 bytes for IPv4 addresses or 16 bytes for IPv6 addresses
$bytes_received
number of bytes received from a client (1.11.4)
$bytes_sent
number of bytes sent to a client
$connection
connection serial number
$hostname
host name
$msec
current time in seconds with the milliseconds resolution
$nginx_version
nginx version
$pid
PID of the worker process
$protocol
protocol used to communicate with the client: TCP or UDP (1.11.4)
$proxy_protocol_addr
client address from the PROXY protocol header, or an empty string otherwise (1.11.4)

The PROXY protocol must be previously enabled by setting the proxy_protocol parameter in the listen directive.

$proxy_protocol_port
client port from the PROXY protocol header, or an empty string otherwise (1.11.4)

The PROXY protocol must be previously enabled by setting the proxy_protocol parameter in the listen directive.

$remote_addr
client address
$remote_port
client port
$server_addr
an address of the server which accepted a connection

Computing a value of this variable usually requires one system call. To avoid a system call, the listen directives must specify addresses and use the bind parameter.

$server_port
port of the server which accepted a connection
$session_time
session duration in seconds with a milliseconds resolution (1.11.4);
$status
session status (1.11.4), can be one of the following:
200
session completed successfully
400
client data could not be parsed, for example, the PROXY protocol header
403
access forbidden, for example, when access is limited for certain client addresses
500
internal server error
502
bad gateway, for example, if an upstream server could not be selected or reached.
503
service unavailable, for example, when access is limited by the number of connections
$time_iso8601
local time in the ISO 8601 standard format
$time_local
local time in the Common Log Format
分享到:
评论

相关推荐

    Nginx反向代理不能访问项目的解决办法

    ### Nginx反向代理不能访问项目的解决办法 在配置Nginx作为反向代理服务器时,有时会遇到可以通过域名正常访问Linux系统下部署的Tomcat服务器,但是却无法访问到部署在Tomcat上的具体项目的情况。这种情况通常是...

    nginx.conf tcp转发、http反向代理

    nginx.conf tcp转发、http反向代理

    nginx的TCP方向代理插件

    在Web服务领域,Nginx以其高效、轻量级的特性被广泛应用于反向代理、负载均衡和静态文件服务器等场景。然而,Nginx默认并不支持TCP层的代理,对于需要在TCP层面进行转发的应用,如MySQL、Redis等数据库连接或SMTP...

    nginx反向代理tcp端口详细操作说明

    nginx反向代理tcp端口详细操作说明,代理所有tcp端口说明

    nginx配置tcp转发(nginx通过白名单访问)

    Nginx作为一款高性能的HTTP和反向代理服务器,不仅在HTTP领域表现出色,还可以用于TCP和UDP的代理转发,扩展了其在网络服务中的应用范围。本文将详细介绍如何配置Nginx进行TCP转发,并探讨相关知识点。 首先,我们...

    nginx-1.14.2-tcp+tcp反向代理模块+check模块

    官方tcp模块,配置stream {} 也可以代理tcp,--with-http_stub_status_module --with-stream https://blog.csdn.net/e_wsq/article/details/79408263 来自GitHub开源提交版及官方公布版,清单如下 nginx_tcp_proxy...

    nginx反向代理配置及优化以及核心讲解以及高性能集群搭建

    Nginx是一款高性能、轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,广泛应用于互联网行业,以其高效稳定、低内存占用和强大的反向代理能力著称。在本文中,我们将深入探讨Nginx的反向代理...

    freeswitch ngnix wss反向代理,jssip配置

    freeswitch支持UDP、TCP、WS(websocket)、WSS方式进行注册,而反向代理是指通过nginx配置,通过WSS的方式连接WS,这样使得freeswitch连接对外是加密的;当然freeswitch本身是支持WSS的, 用ngnix一般除了反向代理,...

    Nginx反向代理实现支持长连接详解

    Nginx与前端的连接默认为长连接,一个用户跟Nginx建立连接之后,通过这个长连接发送多个请求。如果Nginx只是作为reverse proxy的话,可能一个用户连接就需要多个向后端的短连接。如果后端的服务器(源站或是缓存...

    Nginx (一) 正向代理 & 反向代理 及配置

    Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。 Nginx使用基于事件驱动架构,使得其可以支持数以百万级别的TCP连接 高度的模块化和自由软件许可证使得第三方模块...

    Nginx 反向代理

    【Nginx 反向代理】是 Nginx 服务器的一种功能,用于处理客户端的请求。在这种模式下,Nginx 作为客户端和后端服务器之间的中介,接收来自客户端的 HTTP 请求,然后将这些请求转发到内部的 web 服务器进行处理。反向...

    Nginx反向代理实现多Tomcat负载均衡 (1).md

    ### Nginx 反向代理实现多 Tomcat 负载均衡详解 #### 一、基础知识概述 在深入了解如何利用 Nginx 实现对多个 Tomcat 的负载均衡之前,我们首先来简要回顾一下几个核心概念: - **Nginx**:是一款高性能的HTTP和...

    nginx_tcp_proxy_module-master

    Nginx作为一款高性能的HTTP和反向代理服务器,广泛应用于各种Web服务场景。然而,它的功能并不仅限于HTTP和HTTPS协议,通过扩展模块,Nginx也能处理TCP协议的数据转发。"nginx_tcp_proxy_module-master"就是这样一个...

    nginx反向代理时如何保持长连接

    【保持和Client的长连接】 在HTTP 1.1中,长连接通过“Keep-Alive”头字段实现,允许...通过以上配置,可以有效地在Nginx反向代理中实现客户端到Nginx以及Nginx到后端服务器的长连接,从而提升系统性能和资源利用率。

    nginx1.6.3安装tcp支持.zip

    Nginx是一款高性能的HTTP和反向代理服务器,常用于Web服务,它以其轻量级、高并发处理能力而著名。在1.6.3版本中,为了实现TCP负载均衡或处理非HTTP协议的流量,我们需要一个特殊的模块,即nginx_tcp_proxy_module。...

    nginx反向代理配置及优化.doc

    【Nginx反向代理配置及优化】 在服务器性能面临挑战时,Apache服务器可能无法有效应对高并发请求。在这种情况下,引入Nginx作为反向代理可以显著提升系统的并发处理能力。Nginx以其轻量级、高性能的特点,常被用作...

    一分钟了解四层七层反向代理

    - **定义**:四层反向代理工作在网络的传输层(TCP/UDP层),主要关注的是IP地址和端口信息。 - **特点**: - 基于IP地址和端口号进行路由选择。 - 能够实现高效的负载均衡,适用于需要高速转发大量数据流量的场景...

Global site tag (gtag.js) - Google Analytics