如何使用自定义配置运行nginx docker容器? [英] how to run nginx docker container with custom config?

查看:79
本文介绍了如何使用自定义配置运行nginx docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Dockerfile:

Dockerfile:

FROM nginx

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

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;
          }
    }

我运行以下两个命令:

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

然后,我签出了所有正在运行的容器,但没有显示出来.当我搜索nginx容器的日志时,发现以下错误消息:

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:未知指令上游" /etc/nginx/nginx.conf:1 nginx:[emerg]未知指令 /etc/nginx/nginx.conf中的上游":

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

我想念什么?

推荐答案

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

nginx未知指令"upstream" 所述:

As mentioned in nginx unkown directive "upstream":

nginx.conf通常包含该文件时,该文件已经包含在http上下文中:

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

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

您需要使用-c /etc/nginx/nginx.conf或像上面的代码块一样制作一个小的包装,然后nginx -c

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

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

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

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

For a default nginx.conf, check your CMD:

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

这篇关于如何使用自定义配置运行nginx docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆