如何在服务器部署第二个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:
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(orconfig/env/production/server.jsif 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 aurlfield 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.
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:
- Open your site’s Nginx config file (usually at
/etc/nginx/sites-available/mysite). - 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"; } } - Test the config for errors:
sudo nginx -t - Restart Nginx to apply changes:
sudo systemctl restart nginx
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:
- Install pm2 globally:
npm install -g pm2 - Navigate to your first Strapi project folder and start it:
pm2 start npm --name "strapi-first" -- start - Navigate to your second Strapi project folder and start it:
pm2 start npm --name "strapi-second" -- start - Check the running status:
pm2 status - Save the pm2 configuration so projects auto-start on server reboot:
pm2 save && pm2 startup
- 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 likehttps://mysite/second-project/api/posts.
内容的提问来源于stack exchange,提问作者sir-haver




