我有一个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:/etc/nginx/nginx.conf中的未知指令“upstream”:1 nginx:/etc/nginx/enginx.conf中的[emerg]未知指令“上游”:
我错过了什么?
正如NGiNX文档中提到的,< code>upstream应该在< code>http上下文中定义。
如< code > nginx unknown指令“< code>upstream”中所述:
当该文件通常由nginx.conf
包含时,它已经包含在http
上下文中:
http {
include /etc/nginx/sites-enabled/*;
}
你要么需要使用< code >-c/etc/nginx/nginx . conf ,要么做一个类似上面这个块的小包装器,然后< code>nginx -c它。
对于Docker,您可以使用abevoelker/Docker-nginx
看到不同的选项:
docker run -v /tmp/foo:/foo abevoelker/nginx nginx -c /foo/nginx.conf
对于默认的< code>nginx.conf,请检查您的< code>CMD:
CMD ["nginx", "-c", "/data/conf/nginx.conf"]
nginx docker hub官方手册页:
docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
添加gzip配置的示例:
docker run -v [./]gzip.conf:/etc/nginx/conf.d/gzip.conf nginx
或码头工人组成:
version: '3'
services:
nginx:
image: nginx
volumes:
- ./gzip.conf:/etc/nginx/conf.d/gzip.conf
- ./html:/usr/share/nginx/html:ro