0
0
Nginxdevops~30 mins

API routing with location blocks in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
API routing with location blocks
📖 Scenario: You are setting up a simple web server using nginx. Your goal is to route different API requests to different backend services using location blocks.This is like sorting mail into different boxes based on the address on the envelope.
🎯 Goal: Build an nginx configuration that routes requests starting with /api/users to one backend and requests starting with /api/products to another backend.
📋 What You'll Learn
Create an nginx server block with a root path
Add a location block for /api/users routing to http://users_backend
Add a location block for /api/products routing to http://products_backend
Add a default location block for all other requests routing to http://default_backend
💡 Why This Matters
🌍 Real World
Routing API requests to different backend services is common in microservices and web applications.
💼 Career
Understanding nginx location blocks is essential for DevOps roles managing web servers and load balancing.
Progress0 / 4 steps
1
Create the basic server block
Create an nginx server block listening on port 80 with a root path /var/www/html. Use server {} and listen 80; inside it.
Nginx
Need a hint?

Use server {} to start the block, then add listen 80; and root /var/www/html; inside.

2
Add location block for /api/users
Inside the existing server block, add a location block for /api/users that proxies requests to http://users_backend using proxy_pass.
Nginx
Need a hint?

Use location /api/users {} and inside it add proxy_pass http://users_backend;.

3
Add location block for /api/products
Inside the server block, add another location block for /api/products that proxies requests to http://products_backend using proxy_pass.
Nginx
Need a hint?

Add location /api/products {} with proxy_pass http://products_backend; inside.

4
Add default location block and print config
Add a default location block / inside the server block that proxies requests to http://default_backend. Then print the full nginx configuration file content.
Nginx
Need a hint?

Add location / { proxy_pass http://default_backend; } inside the server block.