stream

The 'stream' directive defines a block for handling TCP and UDP traffic in NGINX.

Syntaxstream { ... }
Defaultnone
Contextmain
Argumentsnone

Description

The 'stream' directive allows NGINX to handle non-HTTP traffic, enabling it to facilitate TCP and UDP streams. This directive can be configured in the main context, allowing users to specify different servers or upstreams for various types of streaming data. Inside the 'stream' block, users can define multiple server blocks where each server can handle different types of protocols, ports, or upstream configurations that are distinct from regular HTTP processing.

With the introduction of this directive, users can use various directives such as 'server' and 'proxy_pass' within the 'stream' context to handle streams. For example, TCP load balancing can be set up directly in this block, which skews the usage of resources in favor of different backend servers based on algorithms like round-robin, least connections, or IP hash. Additionally, connection limits, timeout settings, and error handling can also be configured to ensure that the stream processing is both efficient and resilient.

The 'stream' directive opens up NGINX to perform as a more versatile proxy, aiding in various applications such as SSL termination for TCP services, which simplifies secure transports. As a result, this facilitates a wider range of use cases outside the standard HTTP/HTTPS paradigm, enabling applications like databases and game servers to leverage NGINX's capabilities for managing their network functionality effectively.

Config Example

stream {
    upstream backend {
        server backend1.example.com:12345;
        server backend2.example.com:12345;
    }

    server {
        listen 12345;
        proxy_pass backend;
    }
}

Ensure the 'stream' block is not placed within an 'http' or 'server' block, as it must be declared in the main context.

When specifying multiple 'server' blocks within a 'stream' directive, remember to configure the listening ports correctly to avoid conflicts.

← Back to all directives