Bird
Raised Fist0
Nginxdevops~15 mins

Why reverse proxying serves backend applications in Nginx - See It in Action

Choose your learning style10 modes available
Why Reverse Proxying Serves Backend Applications
📖 Scenario: You are setting up a simple web service that runs on a backend server. You want to use nginx as a reverse proxy to forward requests from the internet to your backend application securely and efficiently.
🎯 Goal: Learn how to configure nginx as a reverse proxy to serve a backend application. Understand why reverse proxying is useful for backend services.
📋 What You'll Learn
Create a basic nginx configuration file
Add a server block listening on port 80
Configure nginx to proxy requests to a backend server at http://localhost:5000
Test the configuration by printing the proxy settings
💡 Why This Matters
🌍 Real World
Reverse proxying is used in real websites to protect backend servers, balance load, and improve security.
💼 Career
Understanding reverse proxying is essential for DevOps roles managing web infrastructure and deployments.
Progress0 / 4 steps
1
Create a basic nginx server block
Create a file called nginx.conf and add a server block that listens on port 80.
Nginx
Hint

Use server { listen 80; } to start your configuration.

2
Add reverse proxy configuration
Inside the server block, add a location / block that proxies requests to http://localhost:5000 using proxy_pass.
Nginx
Hint

Use location / { proxy_pass http://localhost:5000; } inside the server block.

3
Explain why reverse proxying helps backend apps
Add a comment inside the server block explaining that reverse proxying helps by hiding backend details and improving security.
Nginx
Hint

Write a comment starting with # Reverse proxy inside the server block.

4
Print the nginx configuration
Print the contents of nginx.conf to show the full reverse proxy setup.
Nginx
Hint

Use print() to display the nginx.conf content exactly.

Practice

(1/5)
1. What is the main purpose of using a reverse proxy like nginx for backend applications?
easy
A. To store user data permanently
B. To replace the backend application entirely
C. To act as a middleman between users and backend servers
D. To directly expose backend servers to the internet

Solution

  1. Step 1: Understand the role of a reverse proxy

    A reverse proxy sits between users and backend servers, forwarding requests and responses.
  2. Step 2: Identify the main purpose

    It improves security and performance by hiding backend details and managing traffic.
  3. Final Answer:

    To act as a middleman between users and backend servers -> Option C
  4. Quick Check:

    Reverse proxy = Middleman [OK]
Hint: Reverse proxy = middleman between user and backend [OK]
Common Mistakes:
  • Thinking reverse proxy replaces backend apps
  • Assuming it stores user data
  • Believing it exposes backend directly
2. Which nginx directive is used to forward client requests to a backend server in reverse proxy setup?
easy
A. proxy_pass
B. server_name
C. listen
D. root

Solution

  1. Step 1: Identify nginx directives related to proxying

    The directive that forwards requests to backend servers is proxy_pass.
  2. Step 2: Differentiate from other directives

    server_name sets domain, listen sets port, root sets file path.
  3. Final Answer:

    proxy_pass -> Option A
  4. Quick Check:

    Forward requests = proxy_pass [OK]
Hint: proxy_pass forwards requests to backend [OK]
Common Mistakes:
  • Confusing proxy_pass with server_name
  • Using listen to forward requests
  • Mistaking root for proxying
3. Given this nginx config snippet, what happens when a user accesses http://example.com/api/?
location /api/ {
    proxy_pass http://backend_server/;
}
medium
A. Requests to /api/ are forwarded to http://backend_server/
B. Requests to /api/ are forwarded to http://backend_server/api/
C. Requests to /api/ are served from nginx root directory
D. Requests to /api/ cause a 404 error

Solution

  1. Step 1: Understand proxy_pass behavior with trailing slash

    When proxy_pass URL ends with a slash, nginx replaces the matching part of the request URI.
  2. Step 2: Apply to given config

    Request to /api/ is forwarded to backend_server root /, so /api/ is replaced by /.
  3. Final Answer:

    Requests to /api/ are forwarded to http://backend_server/ -> Option A
  4. Quick Check:

    proxy_pass with trailing slash replaces matching URI [OK]
Hint: Trailing slash in proxy_pass replaces matching URI part [OK]
Common Mistakes:
  • Assuming URI is appended instead of replaced
  • Thinking nginx serves files locally
  • Expecting 404 without backend
4. You configured nginx as a reverse proxy but get a 502 Bad Gateway error. Which is the most likely cause?
medium
A. server_name directive is incorrect
B. proxy_pass directive is missing a semicolon
C. nginx is listening on the wrong port
D. Backend server is down or unreachable

Solution

  1. Step 1: Understand 502 Bad Gateway meaning

    502 means nginx cannot connect to the backend server to get a valid response.
  2. Step 2: Identify common causes

    The backend server being down or unreachable is the most common cause of 502 errors.
  3. Final Answer:

    Backend server is down or unreachable -> Option D
  4. Quick Check:

    502 error = backend unreachable [OK]
Hint: 502 error usually means backend server unreachable [OK]
Common Mistakes:
  • Blaming missing semicolon for 502 error
  • Confusing port listen issues with backend errors
  • Assuming server_name causes 502
5. You want nginx to serve static files directly but forward API requests to a backend app. Which config snippet correctly achieves this?
hard
A. location / { proxy_pass http://backend/; } location /api/ { root /var/www/html; }
B. location /api/ { proxy_pass http://backend/; } location / { root /var/www/html; }
C. location /api/ { root /var/www/html; } location / { proxy_pass http://backend/; }
D. location / { root /var/www/html; proxy_pass http://backend/; }

Solution

  1. Step 1: Separate static and API requests

    Static files should be served from nginx root for /, API requests proxied under /api/.
  2. Step 2: Check config snippet correctness

    location /api/ { proxy_pass http://backend/; } location / { root /var/www/html; } correctly proxies /api/ to backend and serves / from local files.
  3. Final Answer:

    location /api/ { proxy_pass http://backend/; } location / { root /var/www/html; } -> Option B
  4. Quick Check:

    Static on /, API on /api/ with proxy_pass [OK]
Hint: Proxy API path, serve static root separately [OK]
Common Mistakes:
  • Reversing proxy and root locations
  • Combining proxy_pass and root in same block
  • Serving API requests from root directory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard