Nginx stream and upstream block

nginx

I googled but i did not understand the difference between nginx stream and upstream block for load balancing.

stream
{
    upstream
   {
   }
}

i showed google that some people used stream block, some people ignored for http load balancing but all people used stream block for tcp/udp load balncing.

when i have to use stream block?
when i may ignore stream block?

Best Answer

Since v1.9.0 Nginx can be used as load balancer for any tcp and udp (mysql, dns, etc..)

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

stream {

    upstream  mysql_backend{  
         server 1.1.1.1:3306;
         server unix:/var/lib/mysql.sock;
    }

    server {
         # frontend
         listen 127.0.0.1:3306;
         proxy_pass mysql_backend;
    }
}

So if you want to use nginx as usal (web server only). You don't need this derective. If you need to balance you dns (for example) traffic, use it.