27

I have the latest NGINX from ppa installed on Ubuntu 16.04.

nginx version: nginx/1.12.1

From my understanding, it should support stream and UDP load balancing.

But I get this error message:

nginx: [emerg] "stream" directive is not allowed here in /etc/nginx/conf.d/load-balancer.conf:3

This is my config in /etc/nginx/conf.d/load-balancer.conf

stream {
        upstream backend {
                least_conn;
                server 172.31.9.51 fail_timeout=10s;
                server 172.31.20.140 fail_timeout=10s;
        }

        server {
                listen          500 udp;
                listen          4500 udp;
                proxy_pass      backend;
                proxy_timeout   1s;
                proxy_responses 1;
                error_log       logs/dns.log;
        }
}
Houman
  • 1,325
  • 3
  • 18
  • 30

2 Answers2

53

stream needs to be on the same level as http block so like

http { foo }
stream { bar }

My guess is your include for /etc/nginx/conf.d/*.conf is located in the http {} block and not outside of it. Checkout the /etc/nginx/nginx.conf for the include and maybe you have to make a new one for the stream section

Mike
  • 21,910
  • 7
  • 55
  • 79
  • I've updated the question with the path of my config. `/etc/nginx/conf.d/load-balancer.conf`. You are right! The include is inside the http. – Houman Nov 12 '17 at 17:08
  • Sorry Mike, I have a follow-up issue that I don't understand. Why do I need to specify the port for upstream? `nginx: [emerg] no port in upstream "172.31.9.51"`. Since the UDP ports are defined already under server... – Houman Nov 12 '17 at 17:20
  • 1
    you need to tell it what ports to send back to like `server 172.31.9.51:4500 fail_timeout=10s;` – Mike Nov 12 '17 at 18:49
  • 3
    Interestingly, the default `nginx` Docker image does exactly this: "My guess is your include for /etc/nginx/conf.d/*.conf is located in the http {} block and not outside of it.". So in that Docker image, you cannot use `/etc/nginx/conf.d/*.conf` to add `stream` directives. – javabrett Nov 02 '20 at 22:09
0

I came across the same error with a default nginx Docker image, I tried to solve it with a small change to the DockerFile:

COPY docker/ngnix/ngnix.conf /tmp/docker.nginx
COPY docker/ngnix/stream.conf /etc/nginx/stream.conf
RUN envsubst '$RAILS_ROOT','$DOMAIN' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
RUN echo 'include /etc/nginx/stream.conf;' >> /etc/nginx/nginx.conf

stream.conf

stream{
  server{ 
    ...  
  }
}

I am about to find out if this version of nginx was built with the stream module...

P.S. It deployed with no errors

+1 I am connected from my terminal into Postgres on Kubernetes on AWS through the LoadBalancer and the reverse proxy! Now, SSL...

mekdigital
  • 101
  • 2