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

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

问题描述

我有一个 Dockerfile 和自定义的 nginx 配置文件(和 Dockerfile 在同一目录下)如下:

Dockerfile:

来自 nginx复制 nginx.conf/etc/nginx/nginx.conf

nginx.conf 文件:

上游 myapp1 {最小连接;服务器 http://domain.com:81;服务器 http://domain.com:82;服务器 http://domain.com:83;}服务器 {听 80;地点/{proxy_pass http://myapp1;proxy_http_version 1.1;proxy_set_header 升级 $http_upgrade;proxy_set_header 连接升级";proxy_set_header 主机 $host;proxy_cache_bypass $http_upgrade;}}

我运行这两个命令:

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

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

<块引用>

[emerg] 1#1:未知指令上游"在/etc/nginx/nginx.conf:1 nginx: [emerg] 未知指令/etc/nginx/nginx.conf 中的上游":

我错过了什么?

解决方案

NGiNX 文档upstream 应该在 http 上下文中定义.

nginx 未知指令upstream" 中所述:

<块引用>

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

<代码>http {包括/etc/nginx/sites-enabled/*;}

<块引用>

你要么需要使用 -c/etc/nginx/nginx.conf 要么像上面的块一样制作一个小包装器并 nginx -c 它.

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

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

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

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

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://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:

[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?

解决方案

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"]

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

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