Docker 网络 - nginx:[emerg] 在上游找不到主机
Docker Networking - nginx: [emerg] host not found in upstream
我最近开始迁移到 Docker 1.9 和 Docker-Compose 1.5 的网络功能以替换使用链接。
到目前为止,通过链接,nginx 通过 docker-compose 连接到我的 php5-fpm fastcgi 服务器没有问题,该服务器位于一个组中的不同服务器中。最近虽然当我 运行 docker-compose --x-networking up
我的 php-fpm、mongo 和 nginx 容器启动时,但是 nginx 立即退出 [emerg] 1#1: host not found in upstream "waapi_php_1" in /etc/nginx/conf.d/default.conf:16
但是,如果我 运行 docker-compose 命令再次 php 和 mongo 容器 运行ning(nginx 退出), nginx 启动并从此正常工作。
这是我的 docker-compose.yml
文件:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
这是我的 default.conf
nginx:
server {
listen 80;
root /var/www/test;
error_log /dev/stdout debug;
access_log /dev/stdout;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
# Referencing the php service host (Docker)
fastcgi_pass waapi_php_1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# We must reference the document_root of the external server ourselves here.
fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
如何让 nginx 只使用一个 docker-compose 调用?
将 links 部分添加到您的 nginx 容器配置中。
您必须使 php
容器对 nginx
容器可见。
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
links:
- php:waapi_php_1
通过链接可以强制执行容器启动顺序。没有链接,容器可以按任何顺序启动(或者实际上是一次启动)。
如果 waapi_php_1
容器启动缓慢,我认为旧设置可能会遇到同样的问题。
我认为要使其正常工作,您可以创建一个 nginx 入口点脚本来轮询并等待 php 容器启动并准备就绪。
我不确定 nginx 是否有任何方法可以自动重试与上游的连接,但如果可以,那将是一个更好的选择。
你必须使用 docker-gen 之类的东西在你的后端启动时动态更新 nginx 配置。
参见:
我相信 Nginx+(高级版)也包含解析参数 (http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream)
您可以设置 nginx 的 max_fails 和 fail_timeout 指令来指示 nginx 应该在上游服务器不可用失败之前重试 x 次对容器的连接请求。
您可以根据整个设置的基础架构和速度调整这两个数字。您可以在下面 URL 阅读有关健康检查部分的更多详细信息:
http://nginx.org/en/docs/http/load_balancing.html
以下摘自http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
max_fails=number
sets the number of unsuccessful attempts to communicate with the
server that should happen in the duration set by the fail_timeout
parameter to consider the server unavailable for a duration also set
by the fail_timeout parameter. By default, the number of unsuccessful
attempts is set to 1. The zero value disables the accounting of
attempts. What is considered an unsuccessful attempt is defined by the
proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream,
scgi_next_upstream, and memcached_next_upstream directives.
fail_timeout=time
sets the time during which the specified number of unsuccessful
attempts to communicate with the server should happen to consider the
server unavailable; and the period of time the server will be
considered unavailable. By default, the parameter is set to 10
seconds.
准确的说你修改后的nginx配置文件应该如下所示(这个脚本假设所有的容器都至少启动了25秒,如果没有,请更改fail_timeout或max_fails 在下面的上游部分):
注意:我没有亲自测试脚本,所以你可以试试看!
upstream phpupstream {
server waapi_php_1:9000 fail_timeout=5s max_fails=5;
}
server {
listen 80;
root /var/www/test;
error_log /dev/stdout debug;
access_log /dev/stdout;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
# Referencing the php service host (Docker)
fastcgi_pass phpupstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# We must reference the document_root of the external server ourselves here.
fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
此外,根据 docker (https://github.com/docker/docker.github.io/blob/master/compose/networking.md#update-containers) 的以下注释,很明显,用于检查其他容器健康状况的重试逻辑不是 docker 的责任而不是容器应该自己进行健康检查。
Updating containers
If you make a configuration change to a service and run docker-compose
up to update it, the old container will be removed and the new one
will join the network under a different IP address but the same name.
Running containers will be able to look up that name and connect to
the new address, but the old address will stop working.
If any containers have connections open to the old container, they
will be closed. It is a container's responsibility to detect this
condition, look up the name again and reconnect.
在引入 depends_on 功能(下面讨论)之前,可以使用 "volumes_from" 作为解决方法。您所要做的就是更改 docker-compose 文件,如下所示:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
volumes_from:
- php
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
上述方法中的一个重要警告是 php 的卷暴露给 nginx,这是不希望的。但目前这是一个 docker 可以使用的特定解决方法。
depends_on 特征
这可能是一个未来主义的答案。因为该功能尚未在 Docker 中实现(截至 1.9)
有人提议在 Docker 引入的新网络功能中引入 "depends_on"。但是关于相同的@ https://github.com/docker/compose/issues/374 存在长期的 运行 争论因此,一旦实现,功能 depends_on 可以用于命令容器启动,但目前,您将不得不求助于以下其中一项:
- 让 nginx 重试,直到 php 服务器启动 - 我更喜欢这个
- 使用 volums_from 如上所述的解决方法 - 我会避免使用它,因为体积会泄漏到不必要的容器中。
也许避免链接容器问题的最佳选择是 docker 网络 特性
但为了使这项工作正常进行,docker 在 /etc/hosts 中为每个容器创建条目,这些条目来自分配给每个容器的名称。
with docker-compose --x-networking -up is something like
[docker_compose_folder]-[service]-[incremental_number]
为了不依赖于这些名称的意外更改,您应该使用参数
container_name
在你的docker-compose.yml中如下:
php:
container_name: waapi_php_1
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
确保它与您在配置文件中为此服务分配的名称相同。我很确定有更好的方法可以做到这一点,但这是一个很好的开始方法。
这可以通过提到的 depends_on
指令解决,因为它现在已经实施(2016 年):
version: '2'
services:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- php
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
depends_on:
- mongo
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
成功测试:
$ docker-compose version
docker-compose version 1.8.0, build f3628c7
在 documentation 中查找更多详细信息。
还有一篇非常有趣的文章专门讨论这个话题:Controlling startup order in Compose
我相信 Nginx 没有考虑 Docker 解析器 (127.0.0.11),所以请你尝试添加:
resolver 127.0.0.11
在你的 nginx 配置文件中?
遇到同样的问题,解决了。请将以下行添加到 docker-compose.yml nginx 部分:
links:
- php:waapi_php_1
nginx 配置中的主机 fastcgi_pass 部分应链接到 docker-compose.yml nginx 配置中。
如果您迷路了,请阅读最后一条评论。我找到了另一个解决方案。
主要问题是您命名服务名称的方式。
在这种情况下,如果在您的 docker-compose.yml
中,php 的服务被称为 "api" 或类似的东西,您必须确保在文件 nginx.conf
中以 fastcgi_pass
开头的行与 php 服务同名。即 fastcgi_pass api:9000;
我遇到了同样的问题,因为我的 docker-compose.yml
中定义了两个网络:一个后端和一个前端。
当我将其更改为同一默认网络上的 运行 容器时,一切都开始正常工作。
我的解决方法(经过反复试验):
为了解决这个问题,我必须找到 'upstream' Docker 容器的 全名 通过 运行 docker network inspect my-special-docker-network
并获取上游容器的完整 name
属性:
"Containers": {
"39ad8199184f34585b556d7480dd47de965bc7b38ac03fc0746992f39afac338": {
"Name": "my_upstream_container_name_1_2478f2b3aca0",
然后在 proxy_pass
属性 的 location
块中的 NGINX my-network.local.conf
文件中使用了它:(注意添加了 GUID到容器名称):
location / {
proxy_pass http://my_upsteam_container_name_1_2478f2b3aca0:3000;
相对于以前的工作,但现在坏了:
location / {
proxy_pass http://my_upstream_container_name_1:3000
最可能的原因是最近对 Docker Compose 的更改,在它们的默认容器命名方案中,如 here 所列。
这似乎发生在我和我的团队工作中,最新版本的 Docker nginx
图片:
- 我已经在 docker/compose GitHub here
上与他们一起提出问题
(nginx 新手)
在我的例子中,它是错误的文件夹名称
配置
upstream serv {
server ex2_app_1:3000;
}
确保应用程序文件夹在 ex2 文件夹中:
ex2/app/...
有两件事值得一提:
- 使用同一个网桥
- 使用
links
添加主机解析
我的例子:
version: '3'
services:
mysql:
image: mysql:5.7
restart: always
container_name: mysql
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: tima@123
network_mode: bridge
ghost:
image: ghost:2
restart: always
container_name: ghost
depends_on:
- mysql
links:
- mysql
environment:
database__client: mysql
database__connection__host: mysql
database__connection__user: root
database__connection__password: xxxxxxxxx
database__connection__database: ghost
url: https://www.itsfun.tk
volumes:
- ./ghost-data:/var/lib/ghost/content
network_mode: bridge
nginx:
image: nginx
restart: always
container_name: nginx
depends_on:
- ghost
links:
- ghost
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/letsencrypt:/etc/letsencrypt
network_mode: bridge
如果不指定特殊网桥,所有网桥都使用相同的默认网桥。
乍一看,我错过了,我的 "web" 服务实际上并没有启动,所以这就是 nginx 找不到任何主机的原因
web_1 | python3: can't open file '/var/www/app/app/app.py': [Errno 2] No such file or directory
web_1 exited with code 2
nginx_1 | [emerg] 1#1: host not found in upstream "web:4044" in /etc/nginx/conf.d/nginx.conf:2
出现这个错误是因为我的 php-fpm
图片启用了 cron
,我不知道为什么
我的问题是我忘记指定 网络别名
docker-compose.yml 在 php-fpm
networks:
- u-online
效果不错!
version: "3"
services:
php-fpm:
image: php:7.2-fpm
container_name: php-fpm
volumes:
- ./src:/var/www/basic/public_html
ports:
- 9000:9000
networks:
- u-online
nginx:
image: nginx:1.19.2
container_name: nginx
depends_on:
- php-fpm
ports:
- "80:8080"
- "443:443"
volumes:
- ./docker/data/etc/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/data/etc/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./src:/var/www/basic/public_html
networks:
- u-online
#Docker Networks
networks:
u-online:
driver: bridge
我找到了服务的解决方案,该服务可能因本地开发而被禁用。只需使用变量,防止紧急关闭并在服务可用后工作。
server {
location ^~ /api/ {
# other config entries omitted for breavity
set $upstream http://api.awesome.com:9000;
# nginx will now start if host is not reachable
fastcgi_pass $upstream;
fastcgi_index index.php;
}
}
来源:https://sandro-keil.de/blog/let-nginx-start-if-upstream-host-is-unavailable-or-down/
我最近开始迁移到 Docker 1.9 和 Docker-Compose 1.5 的网络功能以替换使用链接。
到目前为止,通过链接,nginx 通过 docker-compose 连接到我的 php5-fpm fastcgi 服务器没有问题,该服务器位于一个组中的不同服务器中。最近虽然当我 运行 docker-compose --x-networking up
我的 php-fpm、mongo 和 nginx 容器启动时,但是 nginx 立即退出 [emerg] 1#1: host not found in upstream "waapi_php_1" in /etc/nginx/conf.d/default.conf:16
但是,如果我 运行 docker-compose 命令再次 php 和 mongo 容器 运行ning(nginx 退出), nginx 启动并从此正常工作。
这是我的 docker-compose.yml
文件:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
这是我的 default.conf
nginx:
server {
listen 80;
root /var/www/test;
error_log /dev/stdout debug;
access_log /dev/stdout;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
# Referencing the php service host (Docker)
fastcgi_pass waapi_php_1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# We must reference the document_root of the external server ourselves here.
fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
如何让 nginx 只使用一个 docker-compose 调用?
将 links 部分添加到您的 nginx 容器配置中。
您必须使 php
容器对 nginx
容器可见。
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
links:
- php:waapi_php_1
通过链接可以强制执行容器启动顺序。没有链接,容器可以按任何顺序启动(或者实际上是一次启动)。
如果 waapi_php_1
容器启动缓慢,我认为旧设置可能会遇到同样的问题。
我认为要使其正常工作,您可以创建一个 nginx 入口点脚本来轮询并等待 php 容器启动并准备就绪。
我不确定 nginx 是否有任何方法可以自动重试与上游的连接,但如果可以,那将是一个更好的选择。
你必须使用 docker-gen 之类的东西在你的后端启动时动态更新 nginx 配置。
参见:
我相信 Nginx+(高级版)也包含解析参数 (http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream)
您可以设置 nginx 的 max_fails 和 fail_timeout 指令来指示 nginx 应该在上游服务器不可用失败之前重试 x 次对容器的连接请求。
您可以根据整个设置的基础架构和速度调整这两个数字。您可以在下面 URL 阅读有关健康检查部分的更多详细信息: http://nginx.org/en/docs/http/load_balancing.html
以下摘自http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
max_fails=number
sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, and memcached_next_upstream directives.
fail_timeout=time
sets the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable; and the period of time the server will be considered unavailable. By default, the parameter is set to 10 seconds.
准确的说你修改后的nginx配置文件应该如下所示(这个脚本假设所有的容器都至少启动了25秒,如果没有,请更改fail_timeout或max_fails 在下面的上游部分): 注意:我没有亲自测试脚本,所以你可以试试看!
upstream phpupstream {
server waapi_php_1:9000 fail_timeout=5s max_fails=5;
}
server {
listen 80;
root /var/www/test;
error_log /dev/stdout debug;
access_log /dev/stdout;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/.+\.php(/|$) {
# Referencing the php service host (Docker)
fastcgi_pass phpupstream;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# We must reference the document_root of the external server ourselves here.
fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
此外,根据 docker (https://github.com/docker/docker.github.io/blob/master/compose/networking.md#update-containers) 的以下注释,很明显,用于检查其他容器健康状况的重试逻辑不是 docker 的责任而不是容器应该自己进行健康检查。
Updating containers
If you make a configuration change to a service and run docker-compose up to update it, the old container will be removed and the new one will join the network under a different IP address but the same name. Running containers will be able to look up that name and connect to the new address, but the old address will stop working.
If any containers have connections open to the old container, they will be closed. It is a container's responsibility to detect this condition, look up the name again and reconnect.
在引入 depends_on 功能(下面讨论)之前,可以使用 "volumes_from" 作为解决方法。您所要做的就是更改 docker-compose 文件,如下所示:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
volumes_from:
- php
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
上述方法中的一个重要警告是 php 的卷暴露给 nginx,这是不希望的。但目前这是一个 docker 可以使用的特定解决方法。
depends_on 特征 这可能是一个未来主义的答案。因为该功能尚未在 Docker 中实现(截至 1.9)
有人提议在 Docker 引入的新网络功能中引入 "depends_on"。但是关于相同的@ https://github.com/docker/compose/issues/374 存在长期的 运行 争论因此,一旦实现,功能 depends_on 可以用于命令容器启动,但目前,您将不得不求助于以下其中一项:
- 让 nginx 重试,直到 php 服务器启动 - 我更喜欢这个
- 使用 volums_from 如上所述的解决方法 - 我会避免使用它,因为体积会泄漏到不必要的容器中。
也许避免链接容器问题的最佳选择是 docker 网络 特性
但为了使这项工作正常进行,docker 在 /etc/hosts 中为每个容器创建条目,这些条目来自分配给每个容器的名称。
with docker-compose --x-networking -up is something like [docker_compose_folder]-[service]-[incremental_number]
为了不依赖于这些名称的意外更改,您应该使用参数
container_name
在你的docker-compose.yml中如下:
php:
container_name: waapi_php_1
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
确保它与您在配置文件中为此服务分配的名称相同。我很确定有更好的方法可以做到这一点,但这是一个很好的开始方法。
这可以通过提到的 depends_on
指令解决,因为它现在已经实施(2016 年):
version: '2'
services:
nginx:
image: nginx
ports:
- "42080:80"
volumes:
- ./config/docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
depends_on:
- php
php:
build: config/docker/php
ports:
- "42022:22"
volumes:
- .:/var/www/html
env_file: config/docker/php/.env.development
depends_on:
- mongo
mongo:
image: mongo
ports:
- "42017:27017"
volumes:
- /var/mongodata/wa-api:/data/db
command: --smallfiles
成功测试:
$ docker-compose version
docker-compose version 1.8.0, build f3628c7
在 documentation 中查找更多详细信息。
还有一篇非常有趣的文章专门讨论这个话题:Controlling startup order in Compose
我相信 Nginx 没有考虑 Docker 解析器 (127.0.0.11),所以请你尝试添加:
resolver 127.0.0.11
在你的 nginx 配置文件中?
遇到同样的问题,解决了。请将以下行添加到 docker-compose.yml nginx 部分:
links:
- php:waapi_php_1
nginx 配置中的主机 fastcgi_pass 部分应链接到 docker-compose.yml nginx 配置中。
如果您迷路了,请阅读最后一条评论。我找到了另一个解决方案。
主要问题是您命名服务名称的方式。
在这种情况下,如果在您的 docker-compose.yml
中,php 的服务被称为 "api" 或类似的东西,您必须确保在文件 nginx.conf
中以 fastcgi_pass
开头的行与 php 服务同名。即 fastcgi_pass api:9000;
我遇到了同样的问题,因为我的 docker-compose.yml
中定义了两个网络:一个后端和一个前端。
当我将其更改为同一默认网络上的 运行 容器时,一切都开始正常工作。
我的解决方法(经过反复试验):
为了解决这个问题,我必须找到 'upstream' Docker 容器的 全名 通过 运行
docker network inspect my-special-docker-network
并获取上游容器的完整name
属性:"Containers": { "39ad8199184f34585b556d7480dd47de965bc7b38ac03fc0746992f39afac338": { "Name": "my_upstream_container_name_1_2478f2b3aca0",
然后在
proxy_pass
属性 的location
块中的 NGINXmy-network.local.conf
文件中使用了它:(注意添加了 GUID到容器名称):location / { proxy_pass http://my_upsteam_container_name_1_2478f2b3aca0:3000;
相对于以前的工作,但现在坏了:
location / {
proxy_pass http://my_upstream_container_name_1:3000
最可能的原因是最近对 Docker Compose 的更改,在它们的默认容器命名方案中,如 here 所列。
这似乎发生在我和我的团队工作中,最新版本的 Docker nginx
图片:
- 我已经在 docker/compose GitHub here 上与他们一起提出问题
(nginx 新手) 在我的例子中,它是错误的文件夹名称
配置
upstream serv {
server ex2_app_1:3000;
}
确保应用程序文件夹在 ex2 文件夹中:
ex2/app/...
有两件事值得一提:
- 使用同一个网桥
- 使用
links
添加主机解析
我的例子:
version: '3'
services:
mysql:
image: mysql:5.7
restart: always
container_name: mysql
volumes:
- ./mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: tima@123
network_mode: bridge
ghost:
image: ghost:2
restart: always
container_name: ghost
depends_on:
- mysql
links:
- mysql
environment:
database__client: mysql
database__connection__host: mysql
database__connection__user: root
database__connection__password: xxxxxxxxx
database__connection__database: ghost
url: https://www.itsfun.tk
volumes:
- ./ghost-data:/var/lib/ghost/content
network_mode: bridge
nginx:
image: nginx
restart: always
container_name: nginx
depends_on:
- ghost
links:
- ghost
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/letsencrypt:/etc/letsencrypt
network_mode: bridge
如果不指定特殊网桥,所有网桥都使用相同的默认网桥。
乍一看,我错过了,我的 "web" 服务实际上并没有启动,所以这就是 nginx 找不到任何主机的原因
web_1 | python3: can't open file '/var/www/app/app/app.py': [Errno 2] No such file or directory
web_1 exited with code 2
nginx_1 | [emerg] 1#1: host not found in upstream "web:4044" in /etc/nginx/conf.d/nginx.conf:2
出现这个错误是因为我的 php-fpm
图片启用了 cron
,我不知道为什么
我的问题是我忘记指定 网络别名 docker-compose.yml 在 php-fpm
networks:
- u-online
效果不错!
version: "3"
services:
php-fpm:
image: php:7.2-fpm
container_name: php-fpm
volumes:
- ./src:/var/www/basic/public_html
ports:
- 9000:9000
networks:
- u-online
nginx:
image: nginx:1.19.2
container_name: nginx
depends_on:
- php-fpm
ports:
- "80:8080"
- "443:443"
volumes:
- ./docker/data/etc/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
- ./docker/data/etc/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./src:/var/www/basic/public_html
networks:
- u-online
#Docker Networks
networks:
u-online:
driver: bridge
我找到了服务的解决方案,该服务可能因本地开发而被禁用。只需使用变量,防止紧急关闭并在服务可用后工作。
server {
location ^~ /api/ {
# other config entries omitted for breavity
set $upstream http://api.awesome.com:9000;
# nginx will now start if host is not reachable
fastcgi_pass $upstream;
fastcgi_index index.php;
}
}
来源:https://sandro-keil.de/blog/let-nginx-start-if-upstream-host-is-unavailable-or-down/