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

Node.js多实例服务器配置及反向代理使用方法问询

Hey there! Let's tackle your two questions to fix that annoying blocking issue when running MongoDB recalculations. Here's what you need to know:

1. Configuring Multi-Instance Servers on Different Ports

To run separate instances for frontend requests and MongoDB recalculations, you don't need major overhauls to your existing code—just adjust how you handle ports and split your logic:

a. Use Environment Variables for Dynamic Port Assignment

Your current port setup already supports dynamic assignment via environment variables, so you can keep this line as-is:

app.set('port', (process.env.PORT || 3000));

Now start each instance with a unique port:

  • For frontend request handling: PORT=3000 node app.js
  • For MongoDB recalculation tasks: PORT=3001 node app.js

b. Split Business Logic by Server Role

Isolate routes so each server only loads the code it needs to avoid unnecessary bloat:

  • Create a routes/frontend.js file for user-facing routes (page renders, frontend APIs):
    const express = require('express');
    const router = express.Router();
    
    router.get('/', (req, res) => {
      res.render('index'); // Your frontend page logic
    });
    // Add other frontend-focused routes here
    
    module.exports = router;
    
  • Create a routes/recalc.js file for recalculation tasks:
    const express = require('express');
    const router = express.Router();
    
    router.post('/run-recalc', async (req, res) => {
      // Your MongoDB collection recalculation logic here
      try {
        // Execute heavy recalculation
        res.status(200).json({ status: 'Recalculation started' });
      } catch (err) {
        res.status(500).json({ error: err.message });
      }
    });
    
    module.exports = router;
    
  • Load the right routes in your main app.js based on the port:
    // ... existing initialization code ...
    
    const port = app.get('port');
    if (port === 3000) {
      app.use('/', require('./routes/frontend'));
    } else if (port === 3001) {
      app.use('/recalc', require('./routes/recalc'));
    }
    
    app.listen(port, () => {
      const serverType = port === 3000 ? 'frontend request' : 'recalculation task';
      console.log(`${serverType} server started on port ${port}`);
    });
    

For production, tools like PM2 simplify managing multiple instances. Install it first:

npm install pm2 -g

Create an ecosystem.config.js file:

module.exports = {
  apps: [
    {
      name: 'frontend-server',
      script: './app.js',
      env: {
        PORT: 3000,
        NODE_ENV: 'production'
      }
    },
    {
      name: 'recalc-server',
      script: './app.js',
      env: {
        PORT: 3001,
        NODE_ENV: 'production'
      }
    }
  ]
};

Start both instances with:

pm2 start ecosystem.config.js

2. Setting Up a Reverse Proxy for Node.js Servers

You can use either a Node.js-based proxy or Nginx (more common in production) to route traffic to your two servers.

Option 1: Node.js Proxy with http-proxy-middleware

This is great if you want to stay within the Node.js ecosystem:

  1. Install the package:
    npm install http-proxy-middleware --save
    
  2. Create a proxy.js file as your single entry point:
    const express = require('express');
    const { createProxyMiddleware } = require('http-proxy-middleware');
    
    const app = express();
    
    // Forward frontend traffic to port 3000
    app.use('/', createProxyMiddleware({
      target: 'http://localhost:3000',
      changeOrigin: true,
      pathRewrite: { '^/' : '/' }
    }));
    
    // Forward recalculation traffic to port 3001
    app.use('/recalc', createProxyMiddleware({
      target: 'http://localhost:3001',
      changeOrigin: true,
      pathRewrite: { '^/recalc' : '/' }
    }));
    
    app.listen(8080, () => {
      console.log('Reverse proxy running on port 8080');
    });
    

Now your frontend only needs to send requests to http://localhost:8080—the proxy will route them to the correct server automatically.

Option 2: Nginx Reverse Proxy (Production-Grade)

Nginx is more performant for high-traffic scenarios. Here's a basic config:

server {
    listen 80;
    server_name localhost;

    # Route frontend requests to port 3000
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Route recalculation requests to port 3001
    location /recalc {
        proxy_pass http://localhost:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        rewrite ^/recalc(.*)$ $1 break; # Strip the /recalc prefix before forwarding
    }
}

Save this in your Nginx config directory, then restart Nginx to apply changes.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:28:31