如何配置Nginx转发请求至随机端口,实现单EC2多Node.js应用负载均衡
Hey there, let's walk through exactly how to set up Nginx to randomly route requests to your 4 Node.js instances per EC2 server—perfect for utilizing each core effectively alongside your existing AWS EBS load balancer setup.
First, you'll need to run 4 separate Node.js apps on each EC2, each listening on a unique port (e.g., 3000, 3001, 3002, 3003). Using a process manager like PM2 makes this trivial.
Create an ecosystem.config.js file in your app directory to define all instances:
module.exports = { apps : [ { name: "app-3000", script: "./app.js", env: { PORT: 3000 } }, { name: "app-3001", script: "./app.js", env: { PORT: 3001 } }, { name: "app-3002", script: "./app.js", env: { PORT: 3002 } }, { name: "app-3003", script: "./app.js", env: { PORT: 3003 } } ] }
Start all instances with:
pm2 start ecosystem.config.js
You can verify they're running with pm2 list.
Next, update your Nginx config (usually at /etc/nginx/sites-available/your-app.conf or /etc/nginx/nginx.conf) to define an upstream group of your Node.js ports, then set the routing strategy to random.
Here's the full config snippet:
# Define upstream group with all 4 Node.js instances upstream node_local { # Enable random routing (core of what you need) random; # Add each local Node.js instance port server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; server 127.0.0.1:3003; # Optional: Add health checks to skip down instances # server 127.0.0.1:3000 max_fails=3 fail_timeout=30s; # Repeat for other ports if needed } # Main server block to handle incoming requests server { listen 80; server_name your-ec2-public-ip or-your-domain.com; location / { # Forward requests to the upstream group proxy_pass http://node_local; # Critical proxy headers to preserve client info in Node.js proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Always check for config errors before restarting:
nginx -t
If you get a "test is successful" message, restart Nginx to apply changes:
sudo systemctl restart nginx # Or use sudo service nginx restart for older EC2 OS versions
- Weighted Random Routing: If you want to send more traffic to certain instances (e.g., a more powerful core), add a
weightparameter:server 127.0.0.1:3000 weight=2;(this instance gets twice as many requests). - Session Sticky (If Needed): If your app requires session persistence, use
random twoinstead of plainrandom—this will route repeat requests from the same client to the same instance when possible, while still maintaining some randomness. - Monitoring: Use
pm2 monitto track each Node.js instance's resource usage, and check Nginx access logs (/var/log/nginx/access.log) to confirm requests are being distributed randomly.
This setup will work seamlessly with your existing AWS EBS load balancer—EBS handles routing between EC2 instances, and Nginx takes care of distributing traffic across the 4 Node.js apps on each server.
内容的提问来源于stack exchange,提问作者frogengine773

