docker中nginx配置报错解决

bash 复制代码
[root@iZuf6hb2khjn9b2lx76d4tZ ~]# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:45:07 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:45:54 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/12/20 07:47:19 [emerg] 1#1: host not found in upstream "fresh" in /etc/nginx/nginx.conf:29
nginx: [emerg] host not found in upstream "fresh" in /etc/nginx/nginx.conf:29

现在找到了 Docker Nginx 容器启动失败的精准原因 :Nginx 配置文件 /etc/nginx/nginx.conf 的第 29 行中,把 Docker 网络名 fresh 当成了上游服务(upstream)的主机名,但 fresh 是网络名称而非容器名称,导致 Nginx 解析不到这个"主机",触发 host not found in upstream 错误,容器启动失败。

一、错误原因详解

upstream "fresh" 的含义是 Nginx 要反向代理到名为 fresh 的上游服务器,但:

  • fresh 是你创建的 Docker 桥接网络 名称,不是容器名称;
  • Nginx 启动时会尝试解析 fresh 这个主机名,但在 fresh 网络内没有名为 fresh 的容器,解析失败,因此报 host not found,进程直接退出(退出码 1)。

二、解决方案:修正 Nginx 配置文件

核心是把 nginx.conf 第 29 行的 fresh 替换成实际要代理的容器名称/IP (比如你要代理的 material 容器,或 mysql 容器)。

步骤 1:编辑挂载的 Nginx 配置文件
bash 复制代码
# 编辑你挂载的配置文件(/root/nginx/nginx.conf)
vim /root/nginx/nginx.conf
步骤 2:定位并修正第 29 行的 upstream 配置

找到第 29 行类似以下的错误配置:

nginx 复制代码
# 错误示例(把网络名 fresh 当成了容器名)
upstream fresh {
    server fresh:8080;  # 错误:fresh 是网络名,不是容器名
}

将其修正为实际的容器名称/IP (比如你要代理 material 容器,它的端口是 8080):

nginx 复制代码
# 正确示例(替换为容器名 material)
upstream backend {  # 建议重命名 upstream 为有意义的名称,比如 backend
    server material:8080;  # material 是容器名,fresh 网络内可直接通过容器名访问
}

补充:在 Docker 同一网络(fresh)内,容器可以通过容器名称 直接解析 IP,因此 material:8080 能正确访问到该容器的 8080 端口。

步骤 3:保存配置文件并重启 Nginx 容器
bash 复制代码
# 1. 删除启动失败的 nginx 容器
docker rm -f nginx

# 2. 重新启动 Nginx 容器(用之前修正的命令)
docker run -d \
--name nginx \
-p 18080:80 \
-v /root/nginx/html:/usr/share/nginx/html \
-v /root/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /root/nginx/conf.d:/etc/nginx/conf.d \
--network fresh \
nginx

# 3. 查看容器状态(确认 Up 状态)
docker ps

三、验证配置是否生效

bash 复制代码
# 1. 查看 Nginx 容器日志(无报错则成功)
docker logs nginx

# 2. 测试访问 Nginx 代理(访问宿主机 18080 端口)
curl http://localhost:18080
# 正常会返回 material 容器的响应内容(而非 Nginx 默认页面)

四、关键补充说明

  1. Docker 网络内的域名解析规则

    • 同一 Docker 网络内,容器之间可以通过 容器名称容器 IP 访问,不能通过网络名称访问;
    • 比如 fresh 网络内,nginx 容器可以直接访问 material:8080mysql:3306,但不能访问 fresh:8080
  2. 如果不知道要代理的容器名/IP

    执行以下命令查看 fresh 网络内的容器信息:

    bash 复制代码
    docker network inspect fresh | grep -E "Name|IPv4Address"

    输出示例(可获取容器名和 IP):

    复制代码
    "Name": "material",
    "IPv4Address": "172.18.0.3/16",
    "Name": "mysql",
    "IPv4Address": "172.18.0.2/16",

总结

  1. 核心错误:Nginx 配置文件中将 Docker 网络名 fresh 当成了上游容器名,导致域名解析失败。
  2. 解决关键:修改 nginx.conf 第 29 行的 upstream 配置,将 fresh 替换为实际的容器名(如 material)。
  3. 重要规则:Docker 同一网络内,容器通过容器名称解析,而非网络名称。

修正配置后,Nginx 容器就能正常启动,且能正确代理到目标容器。如果仍有报错,可把 nginx.conf 第 29 行附近的配置发给我,我帮你精准修正。

相关推荐
释怀不想释怀9 小时前
Docker(网络)
运维·docker·容器
ZFB000110 小时前
【麒麟桌面系统】V10-SP1 2503 系统知识——进入救援模式
linux·运维·kylin
RisunJan10 小时前
Linux命令-iptables-restore命令(从文件批量恢复 iptables 防火墙规则)
linux·运维·网络
轻造科技10 小时前
周期压缩极限挑战:APS排产优化这样实现
运维·devops
鲨莎分不晴10 小时前
Nginx 部署前端项目实战指南
运维·前端·nginx
hugerat11 小时前
在AI的帮助下,用C++构造微型http server
linux·c++·人工智能·http·嵌入式·嵌入式linux
ha204289419412 小时前
Linux操作系统学习记录之----自定义协议(网络计算器)
linux·网络·学习
想唱rap12 小时前
MYSQL在ubuntu下的安装
linux·数据库·mysql·ubuntu
java_logo12 小时前
QWEN3 企业级 Docker 容器化部署指南
运维·docker·容器·qwen3部署·qwen3部署文档·qwen3部署教程·qwen3部署方案
糖~醋排骨12 小时前
DHCP服务的搭建
linux·服务器·网络