【问题标题】:how to run nginx docker container with custom config?如何使用自定义配置运行 nginx docker 容器?
【发布时间】:2015-07-20 23:58:51
【问题描述】:

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

Dockerfile:

FROM nginx

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

nginx.conf 文件:

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 容器的日志时,我发现了这个错误信息:

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

我错过了什么?

【问题讨论】:

  • 这看起来不像一个合适的主 nginx.conf 文件,您发布的文件必须在 http 指令下,很可能在 /etc/nginx/conf.d/ 目录下添加了一个包含在 http 指令中的行.

标签: nginx docker containers


【解决方案1】:

NGiNX documentation 中所述,upstream 应该在http 上下文中定义。

nginx unkown directive “upstream中所述:

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

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

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

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

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

对于默认的nginx.confcheck your CMD

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

【讨论】:

    【解决方案2】:

    nginx docker hub 官方手册页:

    docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
    

    【讨论】:

      【解决方案3】:

      添加 gzip 配置的示例:

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

      或 docker-compose:

      version: '3'
      
      services:
        nginx:
          image: nginx
          volumes:
            - ./gzip.conf:/etc/nginx/conf.d/gzip.conf
            - ./html:/usr/share/nginx/html:ro
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-09
        • 1970-01-01
        相关资源
        最近更新 更多