Running PHP and Node.js on the same Nginx server lets you serve dynamic PHP applications (like WordPress or Laravel) alongside real-time Node.js services (like WebSockets or APIs) under a single domain and port. This guide walks you through configuring Nginx to route requests to the correct backend based on the URL path or domain, assuming you already have PHP-FPM and Node.js installed and running.
What You’ll Need
- A Linux server (Ubuntu/Debian commands shown) with sudo access
- Nginx installed and running
- PHP-FPM installed and running (e.g., php8.1-fpm)
- Node.js installed, and your Node app listening on a port (e.g., 3000)
- Basic familiarity with the command line and Nginx config files
Step-by-Step
- Confirm both services are running. Check PHP-FPM with
sudo systemctl status php8.1-fpm(adjust version) and your Node app withcurl http://localhost:3000(or your chosen port). Both should respond. - Locate your Nginx site configuration. Usually at
/etc/nginx/sites-available/your-site. If it doesn’t exist, create one and symlink it to/etc/nginx/sites-enabled/. - Open the config file with
sudo nano /etc/nginx/sites-available/your-site. - Set up the server block. Inside
server { ... }, define the server name and root. For example:server { listen 80; server_name example.com; root /var/www/html; index index.php index.html; ... } - Route PHP requests. Add a
locationblock for PHP files. This tells Nginx to pass requests ending in.phpto PHP-FPM:location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; } - Route Node.js requests by path. If you want a specific path (e.g.,
/api) to go to Node, add:location /api/ { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } - Route Node.js by subdomain (alternative). If you prefer a subdomain like
node.example.com, create a separate server block:server { listen 80; server_name node.example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; } } - Handle static files. If you have static assets, let Nginx serve them directly for performance. Add a location block for common static extensions:
location ~*\.(jpg|jpeg|png|gif|css|js|ico)$ { expires 30d; } - Test the configuration. Run
sudo nginx -t. If it says “syntax is ok”, reload Nginx withsudo systemctl reload nginx. - Test the routes. Visit
http://your-domain/index.php(should show PHP) andhttp://your-domain/api/(should hit Node). If you used a subdomain, test that separately.
Common Mistakes
- Wrong PHP-FPM socket path. The path in
fastcgi_passmust match your PHP version. Check withls /var/run/php/. - Node app not listening on the expected port. Ensure your Node server uses
app.listen(3000)and that the port matches theproxy_pass. - Missing WebSocket headers. If your Node app uses WebSockets, you must include the
UpgradeandConnectionheaders as shown, or the connection will fail. - Location order. Nginx picks the longest matching prefix, but regex locations are evaluated in order. Put your PHP regex before any catch-all location.
- Forgetting to reload Nginx. After any config change, run
sudo nginx -tand thensudo systemctl reload nginx.
Tips
- Use a reverse proxy for Node even if it’s on the same machine; it gives you load balancing and security benefits.
- Set
proxy_set_header X-Real-IP $remote_addr;so your Node app sees the client’s real IP, not Nginx’s. - If your Node app serves static files, let Nginx do it instead for better performance. Configure Nginx to serve the static directory directly.
- Use environment variables for ports and paths to make your setup portable.
- Consider using a process manager like PM2 for Node to keep it running across reboots.
Troubleshooting
- 502 Bad Gateway: Usually means Nginx can’t reach PHP-FPM or Node. Check if the service is running (
sudo systemctl status php8.1-fpm,ps aux | grep node) and that the socket/port is correct. - 404 Not Found: If PHP files return 404, your
rootdirective might be wrong, or thetry_filesdirective is missing. Addtry_files $uri $uri/ =404;inside the PHP location. - Node app returns HTML instead of JSON: Your location might be matching the wrong path. Check the
locationblock and the URL you’re testing. - WebSocket connection drops: Ensure the proxy headers are set exactly as shown, and that your Node app handles the upgrade correctly.
- Permission denied on socket: The Nginx user (usually
www-data) must have access to the PHP-FPM socket. Check the socket’s permissions withls -la /var/run/php/.
Frequently Asked Questions
Can I use the same domain for both PHP and Node?
Yes, by using different URL paths. For example, / for PHP and /api for Node. This is the most common setup.
Do I need to change my Node app to work with Nginx?
No, your Node app stays the same. Nginx acts as a reverse proxy, forwarding requests to your Node server’s port.
What if my Node app uses a framework like Express?
Express works perfectly behind Nginx. Just make sure it listens on the port you specify in proxy_pass.
How do I enable HTTPS with this setup?
Use Let’s Encrypt with certbot. It will automatically modify your Nginx config to add SSL and redirect HTTP to HTTPS.
Can I run multiple Node apps on different ports?
Yes, create separate location blocks for each app, each pointing to a different port. Or use different subdomains.
Why is my PHP file downloading instead of executing?
This happens when Nginx doesn’t pass the request to PHP-FPM. Check that the location ~ \.php$ block exists and that fastcgi_pass points to the correct socket.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Choose an Amplifier for Magico A3 Speakers
- How to Ban a Player from Your FiveM Server
- Pokemon Go Friend Codes: How to Add & Use Them33.33 XP/day
- Google Algorithm Change: Original Reporting Boost
- Create Folder for Selected Item on Mac: Full Guide
- Data Center Location Questions for AI Workloads