📖 Scenario: You have a simple Express web server running on your computer. You want to use Nginx as a reverse proxy to forward web requests from port 80 to your Express app running on port 3000. This setup helps manage traffic and improve security.
🎯 Goal: Build a basic Express server and configure Nginx to act as a reverse proxy forwarding requests from port 80 to the Express server on port 3000.
📋 What You'll Learn
Create a simple Express server listening on port 3000
Define a configuration variable for the Express server port
Write the Nginx reverse proxy configuration block
Complete the Nginx config with server and location blocks to forward requests
💡 Why This Matters
🌍 Real World
Using Nginx as a reverse proxy is common in web hosting to manage traffic, improve security, and enable load balancing.
💼 Career
Many web developer and DevOps roles require setting up reverse proxies to deploy Node.js or Express applications in production.
Progress0 / 4 steps
1
Create Express server
Create a file called server.js. Write code to import express, create an app, and make it listen on port 3000. Add a route GET / that sends the text 'Hello from Express!'.
Express
Hint
Use import express from 'express' and app.listen(3000) to start the server.
2
Add port configuration variable
Add a constant variable called PORT and set it to 3000. Change the app.listen call to use this PORT variable.
Express
Hint
Define const PORT = 3000 and use it in app.listen(PORT).
3
Write Nginx reverse proxy config block
Create an Nginx configuration snippet that defines a location / block. Inside it, add the directive proxy_pass http://localhost:3000; to forward requests to the Express server.
Express
Hint
Use location / { proxy_pass http://localhost:3000; } to forward all requests.
4
Complete Nginx server block
Write a full Nginx server block listening on port 80. Inside it, include the location / block with proxy_pass http://localhost:3000;. This completes the reverse proxy setup.
Express
Hint
Wrap the location block inside server { listen 80; ... }.
Practice
(1/5)
1. What is the main role of Nginx when used as a reverse proxy for an Express app?
easy
A. It compiles the Express app code before running.
B. It replaces the Express app and handles all business logic.
C. It stores the database for the Express app.
D. It forwards client requests to the Express server and sends back responses.
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 D