如何使用自定义配置 运行 nginx docker 容器?
how to run nginx docker container with custom config?
我有一个Dockerfile和自定义的nginx配置文件(与Dockerfile在同一个目录)如下:
Docker 文件:
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
然后我检查了所有 运行ning 容器,但它没有出现。当我搜索 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.conf
,check your CMD
:
CMD ["nginx", "-c", "/data/conf/nginx.conf"]
添加gzip配置的示例:
docker run -v [./]gzip.conf:/etc/nginx/conf.d/gzip.conf nginx
或docker-撰写:
version: '3'
services:
nginx:
image: nginx
volumes:
- ./gzip.conf:/etc/nginx/conf.d/gzip.conf
- ./html:/usr/share/nginx/html:ro
nginx 官方 docker hub 手册页:
docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
我有一个Dockerfile和自定义的nginx配置文件(与Dockerfile在同一个目录)如下:
Docker 文件:
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
然后我检查了所有 运行ning 容器,但它没有出现。当我搜索 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 thehttp
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 andnginx -c
it.
在 Docker 的情况下,您可以看到 abevoelker/docker-nginx
的不同选项:
docker run -v /tmp/foo:/foo abevoelker/nginx nginx -c /foo/nginx.conf
对于默认值 nginx.conf
,check your CMD
:
CMD ["nginx", "-c", "/data/conf/nginx.conf"]
添加gzip配置的示例:
docker run -v [./]gzip.conf:/etc/nginx/conf.d/gzip.conf nginx
或docker-撰写:
version: '3'
services:
nginx:
image: nginx
volumes:
- ./gzip.conf:/etc/nginx/conf.d/gzip.conf
- ./html:/usr/share/nginx/html:ro
nginx 官方 docker hub 手册页:
docker run --name my-custom-nginx-container -v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx