Nginx as a reverse proxy helps direct web traffic to your Express app safely and efficiently.
Nginx as reverse proxy in Express
server {
listen 80;
server_name example.com;
location / {
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;
}
}This config listens on port 80 and forwards requests to your Express app on port 3000.
Headers like Host and Connection help keep the connection smooth and secure.
/api/ to a backend running on port 4000.location /api/ {
proxy_pass http://localhost:4000/api/;
}location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
}This simple Express app listens on port 3000. Nginx will forward web requests to it.
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello from Express behind Nginx!'); }); app.listen(3000, () => { console.log('Express app listening on port 3000'); });
Make sure Nginx is installed and running before starting your Express app.
Restart Nginx after changing its config to apply updates: sudo systemctl restart nginx.
Use HTTPS with Nginx for secure connections by adding SSL certificates.
Nginx acts as a middleman between users and your Express app.
It improves security, performance, and flexibility.
Configuring Nginx correctly helps your app handle real-world web traffic smoothly.
Practice
Solution
Step 1: Understand the role of a reverse proxy
A reverse proxy receives client requests and forwards them to a backend server, then returns the server's response to the client.Step 2: Apply this to Nginx and Express
Nginx acts as this middleman, forwarding requests to the Express app and sending back responses, improving security and performance.Final Answer:
It forwards client requests to the Express server and sends back responses. -> Option DQuick Check:
Reverse proxy = forwards requests [OK]
- Thinking Nginx replaces Express app logic
- Confusing reverse proxy with database
- Assuming Nginx compiles code
Solution
Step 1: Identify the correct protocol for Express app
Express apps usually run over HTTP, so the proxy_pass URL must start with http://Step 2: Check the options
proxy_pass http://localhost:3000; uses http://localhost:3000 which is correct; others use wrong protocols like https, ftp, or file.Final Answer:
proxy_pass http://localhost:3000; -> Option BQuick Check:
Express uses HTTP, so proxy_pass must use http:// [OK]
- Using https:// without SSL setup
- Using unsupported protocols like ftp://
- Confusing file paths with URLs
location /api/ {
proxy_pass http://localhost:3000/;
}What happens when a client requests
/api/users?Solution
Step 1: Understand proxy_pass with trailing slash
When proxy_pass URL ends with a slash, Nginx replaces the matching location prefix with the path after the slash.Step 2: Apply to /api/users request
The location /api/ matches, so /api/ is replaced by /, forwarding to http://localhost:3000/users.Final Answer:
Nginx forwards the request to http://localhost:3000/users. -> Option CQuick Check:
Trailing slash in proxy_pass replaces location prefix [OK]
- Assuming proxy_pass appends full path including /api/
- Confusing behavior with or without trailing slash
- Expecting 404 without backend check
Solution
Step 1: Understand 502 Bad Gateway meaning
502 means Nginx cannot connect to the backend server (Express app) to get a valid response.Step 2: Identify common causes
If Express is not running or listening on the wrong port, Nginx cannot forward requests, causing 502 errors.Final Answer:
Express app is not running or not reachable on the specified port. -> Option AQuick Check:
502 = backend unreachable [OK]
- Blaming client browser for server errors
- Ignoring backend server status
- Assuming syntax errors always cause 502
Solution
Step 1: Serve static files directly
Static files under /static/ should be served from disk using root directive.Step 2: Forward API requests to Express
Requests to /api/ should be proxied to Express running on port 4000.Step 3: Match configuration to requirements
location /static/ { root /var/www/html; } location /api/ { proxy_pass http://localhost:4000/; } correctly sets /static/ to serve files and /api/ to proxy_pass Express.Final Answer:
location /static/ { root /var/www/html; } location /api/ { proxy_pass http://localhost:4000/; } -> Option AQuick Check:
Static served by root, API proxied to Express [OK]
- Swapping static and API locations
- Using proxy_pass for static files
- Serving API requests from file root
Ready to prove it?
Pass the test and this pattern moves unknown
