0

我目前有四个容器化的 React + Express 应用程序(端口 3001 暴露),它们位于具有四个不同 CNAME 的四个单独的 ECS 实例上。他们每个人都坐在自己的 nginx 服务后面作为反向代理。

鉴于应用程序的数量可能会增加,我希望使用 ELB 在 ECS 上重新部署它,但我遇到了路径路由问题。我的目标是建立一个系统,<url_name>/service-1将流量路由到引用的容器service-1等。

目前,服务都处于一个running状态,但是路由提供了一堆控制台错误,说 react build 命令生成的静态jscss文件在<url_name>/. 有没有人找到在 ELB 上使用路径路由运行多个全栈 React + Express 应用程序的方法,或者添加 Nginx 服务或将 React 主页更新为固定值的解决方法?

# container_definition
[
  {
    "name": "service-1",
    "image": "<image-name>:latest",
    "cpu": 256,
    "memory": 256,
    "portMappings": [
      {
        "containerPort": 3001,
        "hostPort": 3001
      }
    ],
    "essential": true
  }
]

# rule.json
{
  "ListenerArn": "placeholder",
  "Conditions": [
    {
      "Field": "path-pattern",
      "Values": [
        "/service-1*"
      ]
    }
  ],
  "Priority": 1,
  "Actions": [
    {
      "Type": "forward",
      "TargetGroupArn": "placeholder"
    }
  ]
}

# server.js
const express = require('express'),
path = require('path');

const createServer = function () {

    const port = 3001;

    // Create a new Express server
    const app = express(),
        subpath = express();

    // Ensure every api route is prefixed by /api
    app.use('/api', subpath);

    // All routes related to access points
    const users = require('./routes/users');
    subpath.get('/users/:id', users.getUserById);

    // serve up static assets, i.e. HTML, CSS, and JS from /build
    app.use(express.static('build'));
    if (process.env.NODE_ENV)
        app.get('*', (req, res) => res.sendFile(path.join(__dirname + '/build/index.html')));

    // Start the HTTP listener using the given port
    return app.listen(port, () => console.log(`Express server (HTTP) listening on port ${port}`))
};
module.exports = createServer;
4

0 回答 0