Bird
Raised Fist0
Nginxdevops~5 mins

Why reverse proxying serves backend applications in Nginx - Performance Analysis

Choose your learning style10 modes available
Time Complexity: Why reverse proxying serves backend applications
O(n)
Understanding Time Complexity

We want to understand how the work done by nginx changes when it acts as a reverse proxy for backend servers.

Specifically, how does the number of requests affect the processing time?

Scenario Under Consideration

Analyze the time complexity of the following nginx reverse proxy configuration.


server {
    listen 80;
    location / {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
    

This configuration forwards incoming requests to a backend server while passing client info.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Handling each incoming HTTP request and forwarding it to the backend.
  • How many times: Once per request, repeated for every client connection.
How Execution Grows With Input

Each new request adds one more forwarding operation.

Input Size (n)Approx. Operations
1010 request forwards
100100 request forwards
10001000 request forwards

Pattern observation: The work grows directly with the number of requests.

Final Time Complexity

Time Complexity: O(n)

This means the time to handle requests grows linearly as more requests come in.

Common Mistake

[X] Wrong: "Reverse proxying handles all requests instantly regardless of load."

[OK] Correct: Each request requires processing and forwarding, so more requests mean more work and time.

Interview Connect

Understanding how request load affects reverse proxy performance shows you grasp real-world server behavior and scaling.

Self-Check

What if we added caching to the reverse proxy? How would the time complexity change?

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