36

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;
        }
}
3
  • is that the whole file?
    – Mike
    Nov 12, 2017 at 17:02
  • Yes Mike. This is the whole file.
    – Houman
    Nov 12, 2017 at 17:02
  • The nginx configuration file is located at /etc/nginx/nginx.conf. The stream block is a top-level block. Ensure that files located in the conf.d directory are included into the top-level. Nov 12, 2017 at 17:06

2 Answers 2

66

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

5
  • 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, 2017 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, 2017 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, 2017 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 2, 2020 at 22:09
  • " maybe you have to make a new one for the stream section" what would that look like? currently I have the default /etc/nginx/nginx.conf that already had the include statement for /etc/nginx/conf.d/*.conf include statement.
    – majorgear
    Aug 6, 2023 at 1:42
1

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...

1
  • I utilized the idea of COPY docker/ngnix/stream.conf /etc/nginx/stream.conf
    – DanH
    Aug 11, 2023 at 6:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .