You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Elastic Beanstalk部署多Node.js微服务并配置域名?

Running Multiple Node.js Microservices on Elastic Beanstalk with Custom Domains

Got it, let's break this down into two key parts: getting both your microservices up and running on Elastic Beanstalk (EB), then mapping each to its own custom domain.

Part 1: Deploying Multiple Microservices to a Single EB Environment

If you want to run both services on the same EB instance (to save on costs), you'll need a process manager to handle multiple Node.js processes and a reverse proxy to route traffic correctly. Here's how to set it up:

Step 1: Use PM2 to Manage Multiple Processes

EB's default Node.js setup only monitors a single process, so using pm2 (a production-grade process manager) ensures both services stay running and are properly monitored.

  1. Install pm2 as a project dependency:
    npm install pm2 --save
    
  2. Create an ecosystem.config.js file in your project root to define your services:
    module.exports = {
      apps: [
        {
          name: "service-a",
          script: "./service-a/index.js", // Path to your first service's entry file
          env: {
            NODE_ENV: "production",
            PORT: 3000 // Unique port for service A
          }
        },
        {
          name: "service-b",
          script: "./service-b/index.js", // Path to your second service's entry file
          env: {
            NODE_ENV: "production",
            PORT: 3001 // Unique port for service B
          }
        }
      ]
    };
    
  3. Update your root package.json to use pm2 for startup:
    "scripts": {
      "start": "pm2-runtime start ecosystem.config.js"
    }
    

Step 2: Configure Nginx to Route Traffic by Domain

EB uses Nginx as a reverse proxy by default. You'll need to override its config to route requests based on the incoming domain to the correct service port.

Create a .ebextensions/nginx-microservices.conf file in your project root:

files:
  "/etc/nginx/conf.d/00_microservice_routing.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      # Route traffic for a.api.com to service A (port 3000)
      server {
        listen 80;
        server_name a.api.com;

        location / {
          proxy_pass http://localhost:3000;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }

      # Route traffic for b.api.com to service B (port 3001)
      server {
        listen 80;
        server_name b.api.com;

        location / {
          proxy_pass http://localhost:3001;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }

This file tells Nginx to forward requests to a.api.com to port 3000 and b.api.com to port 3001.

Step 3: Deploy to EB

Package your entire project (including the service-a, service-b, ecosystem.config.js, package.json, and .ebextensions folder) into a zip file and upload it to your EB environment. EB will run the start script, spin up both services via pm2, and use the custom Nginx config to route traffic.


Part 2: Alternative - Deploy to Separate EB Environments (Full Isolation)

If you prefer full isolation (independent scaling, deployments, and monitoring), deploy each microservice to its own EB environment:

  1. Create two separate EB environments (e.g., service-a-env and service-b-env).
  2. For each environment, package only the corresponding service folder (e.g., service-a with its own package.json and index.js) and deploy it to the matching environment. Each service will run on EB's default port (80) in its own environment.
  3. Map each domain to its respective EB environment's load balancer DNS (see the next section for domain setup).

Part 3: Mapping Custom Domains to Your Services

Regardless of whether you use a single or separate EB environments, you'll need to point your domains to EB and configure SSL (optional but highly recommended):

Step 1: Point Domains to EB

Using your domain registrar or AWS Route 53:

  • For a single EB environment: Create two CNAME records pointing a.api.com and b.api.com to your EB environment's load balancer DNS name (found in the EB console under "Configuration" > "Load balancer").
  • For separate EB environments: Create a CNAME record for each domain pointing to its respective environment's load balancer DNS.

To use HTTPS, request an SSL certificate from AWS Certificate Manager (ACM) for both a.api.com and b.api.com. Then:

  • For a single environment: Update your Nginx config to listen on port 443 and use the ACM certificate.
  • For separate environments: In each EB environment's "Configuration" > "Load balancer", enable HTTPS and attach the ACM certificate.

Key Notes

  • Port Management: Ensure your microservices listen on unique ports (3000, 3001, etc.) if running on the same instance. EB's security group only needs to open ports 80/443 (since Nginx handles internal routing).
  • Process Monitoring: PM2 will restart any crashed services automatically, and EB will monitor the pm2 process to ensure the environment stays healthy.
  • Isolation vs. Cost: Separate environments offer better isolation but incur higher costs. A single environment is more cost-effective but ties both services to the same instance resources.

内容的提问来源于stack exchange,提问作者Martin D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 09:12:41