如何在同一VPS与域名部署静态前端及Node.js Express服务
Hey there! Let's get your landing page and Express email service up and running on the same VPS and domain without any hiccups. Here's exactly what you need to do:
First, we don't want the Express service to clash with Nginx (which is handling your frontend). Let's have Express listen on a different port that's only accessible locally (for security). Open your server.js file and update the listen line to something like:
app.listen(3001, 'localhost', () => { console.log('Email service running on localhost:3001'); });
Binding to localhost ensures the service isn't directly exposed to the internet—only Nginx will be able to reach it.
Since your frontend is already served via Nginx, we'll add a reverse proxy rule to route API requests to your Express service.
Open your Nginx domain configuration file (usually located at /etc/nginx/sites-available/your-domain.conf) and update the server block with these additions:
server { listen 80; server_name your-domain.com www.your-domain.com; # Serve your static frontend files root /path/to/your/frontend/files; # e.g., /var/www/landing-page index index.html; # Proxy all /api requests to your Express server location /api { 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; proxy_set_header X-Forwarded-Proto $scheme; } # Optional: Handle SPA routing (if your frontend uses client-side routes) location / { try_files $uri $uri/ /index.html; } }
- Replace
/path/to/your/frontend/fileswith the actual directory where you uploaded your static files (index.html, css/, js/, img/). - Now, when your frontend sends a request like
https://your-domain.com/api/send-email, Nginx will forward it tohttp://localhost:3001/api/send-email—no more CORS issues since everything is under the same domain!
Running node server.js directly will stop the service when you close your terminal. Use pm2, a process manager for Node.js, to keep it running in the background:
- Install pm2 globally:
sudo npm install -g pm2 - Navigate to your Express service's directory and start it:
pm2 start server.js --name email-service - Set pm2 to start on boot:
pm2 startup ubuntuthen run the command it outputs to enable auto-start - Check the service status anytime with:
pm2 status - Restart the service if you make changes:
pm2 restart email-service
- Restart Nginx to apply the new config:
sudo systemctl restart nginx - Visit your domain in a browser, fill out the form, and submit it to confirm the email sends correctly.
- If something breaks, check the logs:
- Nginx error logs:
sudo tail -f /var/log/nginx/error.log - pm2 service logs:
pm2 logs email-service
- Nginx error logs:
Secure your site with a free Let's Encrypt certificate:
- Install Certbot and its Nginx plugin:
sudo apt update && sudo apt install certbot python3-certbot-nginx - Run the setup wizard:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com - Follow the prompts—Certbot will automatically update your Nginx config to redirect HTTP to HTTPS and install the certificate.
内容的提问来源于stack exchange,提问作者Dmitry Klymenko

