📖 Scenario: You are working on a web application that has a frontend and a backend API. The backend API runs on a different server or port. You want to use Nginx as a reverse proxy to route API requests to the backend server while serving the frontend files directly.This setup helps keep your application organized and improves performance by letting Nginx handle routing efficiently.
🎯 Goal: Learn how to configure Nginx to route API requests to a backend server while serving frontend files from the same domain.
📋 What You'll Learn
Create a basic Nginx server block to serve frontend files
Add a variable to define the backend API server address
Configure Nginx to route requests starting with /api/ to the backend API server
Print the final Nginx configuration to verify the routing setup
💡 Why This Matters
🌍 Real World
Nginx is often used as a reverse proxy to route API requests to backend servers while serving frontend files. This helps organize traffic and improve performance.
💼 Career
Understanding how to configure Nginx for API routing is a key skill for DevOps engineers and backend developers working with web applications.
Progress0 / 4 steps
1
Create a basic Nginx server block
Write an Nginx server block that listens on port 80 and serves static files from /var/www/html for all requests.
Nginx
Hint
Use server {} block with listen 80; and location / { root /var/www/html; index index.html; }.
2
Add a variable for the backend API server
Inside the server block, add a variable called $backend_api and set it to http://localhost:5000 to represent the backend API server address.
Nginx
Hint
Use set $backend_api http://localhost:5000; inside the server block.
3
Configure API routing to the backend server
Add a location /api/ {} block inside the server block that proxies requests to the backend API server using the $backend_api variable. Use proxy_pass $backend_api; inside this location.
Nginx
Hint
Use location /api/ { proxy_pass $backend_api; } inside the server block.
4
Print the final Nginx configuration
Print the entire Nginx configuration to verify that the server block listens on port 80, serves static files from /var/www/html, and routes /api/ requests to $backend_api.
Nginx
Hint
Print the full configuration text exactly as shown.
Practice
(1/5)
1. Why is Nginx commonly used to handle API routing in web applications?
easy
A. Because it replaces the need for backend servers
B. Because it efficiently directs API requests to the correct backend services
C. Because it stores API data permanently
D. Because it automatically writes API documentation
Solution
Step 1: Understand Nginx's role in routing
Nginx acts as a traffic controller, sending API requests to the right backend service based on rules.
Step 2: Identify what Nginx does not do
Nginx does not store data, replace backend servers, or write documentation; it only routes requests.
Final Answer:
Because it efficiently directs API requests to the correct backend services -> Option B
Quick Check:
Routing = directing requests [OK]
Hint: Nginx routes requests, it doesn't store or create data [OK]
Common Mistakes:
Thinking Nginx stores API data
Believing Nginx replaces backend servers
Assuming Nginx writes API docs
2. Which Nginx directive is used to forward API requests to a backend server?
easy
A. proxy_pass
B. listen
C. server_name
D. root
Solution
Step 1: Identify the directive for forwarding requests
The proxy_pass directive tells Nginx where to send the incoming API requests.
Step 2: Differentiate from other directives
listen sets the port, server_name sets the domain, and root sets the file path, none forward requests.
Final Answer:
proxy_pass -> Option A
Quick Check:
Forwarding = proxy_pass [OK]
Hint: proxy_pass sends requests to backend servers [OK]
Common Mistakes:
Confusing listen with proxy_pass
Using root to forward requests
Mixing server_name with routing
3. Given this Nginx config snippet, what backend URL will a request to /api/users be forwarded to?
Hint: Always add trailing slash in proxy_pass for correct routing [OK]
Common Mistakes:
Ignoring trailing slash importance
Thinking location path needs trailing slash
Assuming HTTPS is mandatory
5. You want Nginx to route requests starting with /api/v1/ to http://v1-backend:9000/ and requests starting with /api/v2/ to http://v2-backend:9000/. Which configuration correctly achieves this?