57

I have a Dockerfile and custom Nginx configuration file (in the same directory with Dockerfile) as follows:

Dockerfile:

FROM nginx

COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf file:

upstream myapp1 {
          least_conn;
          server http://example.com:81;
          server http://example.com:82;
          server http://example.com:83;
    }

server {
          listen 80;

          location / {
            proxy_pass http://myapp1;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
          }
    }

I run these two commands:

docker --tls build -t nginx-image .
docker --tls run -d -p 80:80 --name nginx nginx-image

Then I checked out all running containers but it didn't show up. When I searched Nginx container's log, I found this error message:

[emerg] 1#1: unknown directive "upstream" in /etc/nginx/nginx.conf:1 Nginx: [emerg] unknown directive "upstream" in /etc/nginx/nginx.conf:

What am I missing?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Abdurrahman Alp Köken
  • 1,236
  • 1
  • 13
  • 12
  • This doesn't look like a proper main nginx.conf file, the file you posted has to be under `http` directive, most likely under `/etc/nginx/conf.d/` directory added an an include line within http directive. – Daniel t. May 10 '15 at 12:54

3 Answers3

23

As mentioned in the NGiNX documentation, upstream is supposed to be defined in an http context.

As mentioned in nginx unkown directive “upstream:

When that file is included normally by nginx.conf, it is included already inside the http context:

http {
  include /etc/nginx/sites-enabled/*;
}

You either need to use -c /etc/nginx/nginx.conf or make a small wrapper like the above block and nginx -c it.

In case of Docker, you can see different options with abevoelker/docker-nginx:

docker run -v /tmp/foo:/foo abevoelker/nginx nginx -c /foo/nginx.conf

For a default nginx.conf, check your CMD:

CMD ["nginx", "-c", "/data/conf/nginx.conf"]
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
18

official nginx docker hub manual page:

docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
rojen
  • 399
  • 3
  • 4
5

An example for adding gzip config:

docker run -v [./]gzip.conf:/etc/nginx/conf.d/gzip.conf nginx

or docker-compose:

version: '3'

services:
  nginx:
    image: nginx
    volumes:
      - ./gzip.conf:/etc/nginx/conf.d/gzip.conf
      - ./html:/usr/share/nginx/html:ro
multipolygon
  • 2,194
  • 2
  • 19
  • 23