nginx: [emerg] "stream" directive is not allowed here

53,231

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

Share:
53,231

Related videos on Youtube

Houman
Author by

Houman

Updated on September 18, 2022

Comments

  • Houman
    Houman almost 2 years

    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;
            }
    }
    
    • Mike
      Mike over 6 years
      is that the whole file?
    • Houman
      Houman over 6 years
      Yes Mike. This is the whole file.
    • Richard Smith
      Richard Smith over 6 years
      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.
  • Houman
    Houman over 6 years
    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
    Houman over 6 years
    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...
  • Mike
    Mike over 6 years
    you need to tell it what ports to send back to like server 172.31.9.51:4500 fail_timeout=10s;
  • javabrett
    javabrett over 3 years
    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.