[英]How to run Nginx docker container with custom config?
我有一個 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/nginx.conf
中的[emerg] 未知指令“upstream”:
我錯過了什么?
如NGiNX 文檔中所述, upstream
應該在http
上下文中定義。
正如nginx
未知指令“ 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.conf
, 請檢查您的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.