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 {
                server fail_timeout=10s;
                server fail_timeout=10s;

        server {
                listen          500 udp;
                listen          4500 udp;
                proxy_pass      backend;
                proxy_timeout   1s;
                proxy_responses 1;
                error_log       logs/dns.log;
  • 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


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

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

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



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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.