gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:46:27 29 4
gpt4 key购买 nike

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

Dockerfile:

FROM nginx

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

nginx.conf 文件:

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

我运行这两个命令:

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

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

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

我错过了什么?

最佳答案

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

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.

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

关于nginx - 如何使用自定义配置运行 Nginx docker 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151436/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com