【问题标题】:Nginx, Docker and Docker ComposeNginx、Docker 和 Docker 组合
【发布时间】:2020-08-01 03:03:12
【问题描述】:

我正在尝试为个人博客设置微服务架构。

这个想法是让 NGINX 容器为静态 gatsby 站点提供服务,并重定向到其他服务。例如,我想在 /todos 有一个 react 应用,在 /todos_api 有一个用于该 todo 应用的 api。

我目前的文件夹结构是这样的:

  • docker-compose.yml
  • 盖茨比博客
    • (包含构建文件夹)
    • nginx
      • default.conf(这是我的主要 nginx 条目)
  • 投资组合
    • 待办事项
      • todo_client
        • nginx
          • default.conf(这仅用于为 react 应用提供服务)
      • todo_api

我的 docker-compose 文件如下所示:

version: "3"

services:
  gatsby:
    restart: always
    build:
      dockerfile: Dockerfile
      context: ./gatsby_blog
    ports:
      - "80:80"
  todoclient:
    build:
      dockerfile: Dockerfile
      context: ./portfolio/todos/todo_client

我的 Gatsby nginx 主文件如下所示:

upstream todoclient {
  server todoclient:3000;
}

server {
  listen 80;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }

  location /todos {
      rewrite /todos/(.*) /$1 break;
      proxy_pass http://todoclient;
  }
}

我的 React nginx 配置是这样的:

server {
  listen 3000;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
}

我很确定的问题是我的 nginx 配置。当我转到 localhost 时,我遇到了 gatsby 应用程序,但是如果我转到 /todos,我会收到一个 nginx 错误。可以看到请求正确传递到了todoclient容器,但是返回的错误是:

open() "/usr/share/nginx/html/todos" 失败(2:没有那个文件或目录

如果有人能看出我在 nginx 配置上哪里出了问题,我将不胜感激。如果需要,我也可以发布我的 Dockerfile。 谢谢

编辑

我现在已经设法让代理工作了,但问题是 todos 应用程序找不到它的静态文件。它们在容器中的正确位置,并且容器独立工作,因此问题与 docker-compose 和 nginx 代理有关。

【问题讨论】:

    标签: reactjs docker nginx docker-compose gatsby


    【解决方案1】:

    为角完成它。 nginx 文件应该替换 /etc/nginx/nginx.conf 中的文件。并将 todos 文件夹(假设有静态页面)放在 /usr/share/nginx/html 中。您可以一次尝试一项服务

    
    
    http {
    
    
        server {
            listen 80;
            server_name  localhost;
    
            root   /usr/share/nginx/html;
            include /etc/nginx/mime.types;
    
    
            location /todos {
                alias /usr/share/nginx/html/todos/;
                absolute_redirect off;
                rewrite ^(.+)/todos/+$ $1 permanent;
                rewrite ^(.+)/todos/index.html$ $1 permanent;    
    
    
                try_files $uri$args $uri$args/ $uri/ /todos/index.html;
    
            }
    
        }
    }
    

    【讨论】:

    • 谢谢你 - 我发现我的问题是尾部斜杠......但现在我的 Javascript 得到了 404,即使它在正确的位置。我会试试你的配置,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2021-03-14
    • 2020-07-23
    相关资源
    最近更新 更多