You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在服务器部署第二个Strapi项目并实现多项目同时运行

Nice one! Running multiple Strapi projects under the same domain with subdirectories is totally feasible, but it needs a few key tweaks—adjusting each Strapi instance's settings and setting up a reverse proxy to route requests properly. Let’s walk through this step by step:

Step 1: Configure Your Second Strapi Project’s Port & Base URL

First, navigate to your second Strapi project folder on the server. You need to adjust two critical settings to avoid conflicts with your first project:

  • Change the listening port: Open config/server.js (or config/env/production/server.js if you’re running in production mode) and set a unique port (your first project is likely using 1337, so let’s use 1338 here):

    module.exports = ({ env }) => ({
      host: env('HOST', '0.0.0.0'),
      port: env('PORT', 1338), // Unique port for the second project
      app: {
        keys: env.array('APP_KEYS'),
      },
    });
    
  • Set the public base URL: Still in config/server.js, add a url field pointing to your subdirectory. This tells Strapi to prefix all its internal routes, API endpoints, and admin UI paths with /second-project:

    module.exports = ({ env }) => ({
      host: env('HOST', '0.0.0.0'),
      port: env('PORT', 1338),
      url: env('PUBLIC_URL', 'https://mysite/second-project'), // Match your subdirectory URL
      app: {
        keys: env.array('APP_KEYS'),
      },
    });
    

    This ensures links and resources in the second project’s admin UI and API work correctly under the subdirectory.

Step 2: Set Up a Reverse Proxy (Nginx Example)

You can’t access Strapi via a subdirectory directly using its port—you need a reverse proxy (like Nginx) to route requests from mysite/second-project to your second Strapi instance’s port (1338), while sending root path requests to your first project (1337).

Here’s how to configure Nginx:

  1. Open your site’s Nginx config file (usually at /etc/nginx/sites-available/mysite).
  2. Add these location blocks to your server configuration:
    server {
        listen 80;
        server_name mysite;
    
        # Route root path to your first Strapi project (port 1337)
        location / {
            proxy_pass http://localhost:1337;
            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;
        }
    
        # Route /second-project to your second Strapi project (port 1338)
        location /second-project {
            proxy_pass http://localhost:1338/second-project; # Important: include the subdirectory suffix here
            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;
    
            # Handle WebSocket connections (used by Strapi's admin UI)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    
  3. Test the config for errors: sudo nginx -t
  4. Restart Nginx to apply changes: sudo systemctl restart nginx
Step 3: Run Both Projects in the Background (Production-Friendly)

Using strapi start or npm start directly in the terminal will stop the project if you close the session. For production, use a process manager like pm2 to keep both projects running 24/7:

  1. Install pm2 globally: npm install -g pm2
  2. Navigate to your first Strapi project folder and start it:
    pm2 start npm --name "strapi-first" -- start
    
  3. Navigate to your second Strapi project folder and start it:
    pm2 start npm --name "strapi-second" -- start
    
  4. Check the running status: pm2 status
  5. Save the pm2 configuration so projects auto-start on server reboot:
    pm2 save && pm2 startup
    
Quick Checks to Avoid Issues
  • Make sure both Strapi projects use separate databases (or at least separate table prefixes) to prevent data conflicts.
  • If you’re using HTTPS, update your Nginx config to listen on port 443 and add your SSL certificate details.
  • Test the second project by visiting https://mysite/second-project/admin—ensure the admin UI loads correctly and you can access API endpoints like https://mysite/second-project/api/posts.

内容的提问来源于stack exchange,提问作者sir-haver

火山引擎 最新活动