docker-compose 配置nginx

前言

前端打包的dist文件在宿主机,nginx运行在docker-compose

问题

nginx.conf 在本地配置可以生效,但是链接到容器就报错

基于本地的nginx运行,本地nginx.conf 如下

复制代码
server {
    listen 8081;
    location / {
        root /usr/local/software/testweb/dist; # 指向你的Vue应用解压后的目录,宿主机目录
        try_files $uri $uri/ /index.html; # 尝试直接找到文件,如果找不到则回退到index.html(Vue路由需要)
    }

}

基于容器运行,映射到容器的nginx.conf配置如下

复制代码
server {
    listen 80;
    root /usr/share/nginx/html; #容器内的地址
    location / {
        try_files $uri $uri/ /index.html; # 尝试直接找到文件,如果找不到则回退到index.html(Vue路由需要)
    }

}

容器内部目录截图如下

容器的docker-compose配置如下

复制代码
version: '3.8'
services:
  nginx:
    image: nginx:latest
    ports:
    volumes:
      - /usr/local/software/dist:/usr/share/nginx/html 
这里是将前面的本地dist路径,挂载到容器,然后上面的nginx.conf 的 root /usr/share/nginx/html; 容器内的地址,才会生效
相关推荐
`Levie7 小时前
Docker-安装部署、容器管理(一)
docker·容器·eureka
op_qo10 小时前
写了几年 OpenResty Lua 网关脚本,聊聊绕不开的 5 个痛点与一种新解法
nginx
Diligently_11 小时前
第 08 篇|单机部署:本地安装与 Docker 最佳实践
运维·spring boot·docker
Johny_Zhao1 天前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
FungLeo1 天前
FengCMS·PHP+MySQL 升级实录 ·本地容器环境设计:只读挂载、LOCAL_UID、数据卷权限
开发语言·mysql·docker·php·fengcms
Csvn1 天前
Nginx 可观测性与工程化:指标、告警与发布回滚演练
后端·nginx
Diligently_1 天前
第 05 篇|慢查询优化实战 —— 一条 SQL 的重构全过程
容器
cpolar技术支持2 天前
用一份 YAML 画出家里的网络拓扑:哪个口通公网、哪几个容器藏在内网,一张图摊开,再用 cpolar 在外网随时翻这份图
docker·网络拓扑·内网穿透·cpolar·yaml
赤生云电竞2 天前
算力调度系统架构实践:计费、配额与抢占策略设计——面向Kubernetes GPU集群的多租户治理
容器·系统架构·kubernetes
Csvn2 天前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx