如何在Elastic Beanstalk部署多Node.js微服务并配置域名?
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.
- Install pm2 as a project dependency:
npm install pm2 --save - Create an
ecosystem.config.jsfile 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 } } ] }; - Update your root
package.jsonto 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:
- Create two separate EB environments (e.g.,
service-a-envandservice-b-env). - For each environment, package only the corresponding service folder (e.g.,
service-awith its ownpackage.jsonandindex.js) and deploy it to the matching environment. Each service will run on EB's default port (80) in its own environment. - 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.comandb.api.comto 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.
Step 2: Configure SSL (Optional but Recommended)
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

