nginx server_name inside stream block possible?
Asked Answered
M

3

23

Current setup as follows:

stream {
    server {
        listen 9987 udp;

        server_name  subdomain.EXAMPLE.com; # this line is resulting in an error

        proxy_pass localhost:9987;
        proxy_timeout 1s;
        proxy_responses 1;
        error_log logs/dns.log;
    }
}

server_name subdomain.EXAMPLE.com;

Is this possible?

$nginx -t

$nginx: [emerg] "server_name" directive is not allowed here in /etc/nginx/nginx.conf:15

Works just fine without server_name, but I'd like to use a sub-domain if possible. (I am using a build with --with-stream, thats not my issue.)

Merengue answered 21/7, 2017 at 1:18 Comment(1)
An alternative approach that may work for some is to bind against a specific IP address: listen x.x.x.x:9987 udpOsiris
M
28

TCP has no concept of server names, so this is not possible. It only works in HTTP because the client sends the hostname it is trying to access as part of the request, allowing nginx to match it to a specific server block.

Source: https://forum.nginx.org/read.php?2,263208,263217#msg-263217

Merengue answered 21/7, 2017 at 1:31 Comment(1)
Hello, this seems unexact as another post indicates that Nginx has the ability to read the SNI header in the SSL handshake before passing the request to the upstream server : serverfault.com/a/999428Geralyngeraniaceous
U
4

While it is not in the original nginx, it is implemented in a Chinese fork of it via SNI: https://tengine.taobao.org/document/stream_sni.html

Underscore answered 1/7, 2022 at 19:20 Comment(0)
S
1

It's now possible, as of NGINX 1.25.5

More info here: https://nginx.org/en/docs/stream/ngx_stream_core_module.html#server_name

Sachiko answered 18/4, 2024 at 7:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.