如何使用自定义配置运行nginx docker容器?

时间:2022-05-08 20:45:41

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

我有一个Dockerfile和自定义nginx配置文件(与Dockerfile在同一目录中),如下所示:

Dockerfile:

FROM nginx

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

nginx.conf file:

upstream myapp1 {
          least_conn;
          server http://domain.com:81;
          server http://domain.com:82;
          server http://domain.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:

然后我检查了所有正在运行的容器,但它没有显示出来。当我搜索nginx容器的日志时,我发现此错误消息:

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

[emerg] 1#1:/etc/nginx/nginx.conf中的未知指令“upstream”:1 / nginx:/ emerc] /etc/nginx/nginx.conf中的[upstream]未知指令“upstream”:

What am I missing?

我错过了什么?

1 个解决方案

#1


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

如NGiNX文档中所述,上游应该在http上下文中定义。

As mentioned in nginx unkown directive “upstream:

如nginx unkown指令“upstream”中所述:

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

当nginx.conf正常包含该文件时,它已包含在http上下文中:

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.

您需要使用-c /etc/nginx/nginx.conf或者像上面的块和nginx -c那样制作一个小包装器。

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

对于Docker,您可以看到abevoelker / docker-nginx的不同选项:

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

For a default nginx.conf, check your CMD:

对于默认的nginx.conf,请检查您的CMD:

CMD ["nginx", "-c", "/data/conf/nginx.conf"]

#1


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

如NGiNX文档中所述,上游应该在http上下文中定义。

As mentioned in nginx unkown directive “upstream:

如nginx unkown指令“upstream”中所述:

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

当nginx.conf正常包含该文件时,它已包含在http上下文中:

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.

您需要使用-c /etc/nginx/nginx.conf或者像上面的块和nginx -c那样制作一个小包装器。

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

对于Docker,您可以看到abevoelker / docker-nginx的不同选项:

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

For a default nginx.conf, check your CMD:

对于默认的nginx.conf,请检查您的CMD:

CMD ["nginx", "-c", "/data/conf/nginx.conf"]