Module 66: Web Server Configuration

Lesson 3 of 5 · 15–25 minutes

Reverse proxy to your API

🏠The everyday picture

The hotel front desk takes your request for room service and passes it to the kitchen, then brings back what the kitchen sends. You never walk into the kitchen yourself.

📘What it really is

location /v1/ {
    proxy_pass http://127.0.0.1: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;
}

🖱️Try it

Add the /v1/ location to your server block, pointing at your API running under systemd. Make sure the API listens on 127.0.0.1 only. Reload Nginx and use the app from the browser at http://crm.lab, frontend and API through one address. Check the API logs show real client addresses after setting trust proxy.

✅Quick check

Why make the API listen on 127.0.0.1 instead of all addresses?

Show the answer

So it can only be reached through Nginx on the same server, not directly from the internet.

Why set trust proxy in Express behind Nginx?

Show the answer

So the app sees the real visitor's address instead of the proxy's, for logs and rate limiting.

← PreviousServer blocks (virtual hosts)
ProLearners Assist