Bird
Raised Fist0
Nginxdevops~10 mins

proxy_pass directive in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - proxy_pass directive
Client sends HTTP request
↓
Nginx receives request
↓
Check location block with proxy_pass
↓
Rewrite request URL if needed
↓
Forward request to backend server
↓
Backend server processes request
↓
Backend sends response to Nginx
↓
Nginx sends response back to client
The proxy_pass directive tells Nginx to forward client requests to another server and then return the response back to the client.
Execution Sample
Nginx
location /app/ {
    proxy_pass http://backend_server/;
}
This config forwards requests starting with /app/ to the backend_server, replacing the /app/ prefix with /.
Process Table
StepClient Request URLLocation Matchproxy_pass TargetRewritten URLAction
1/app/page1/app/http://backend_server//page1Forward to http://backend_server/page1
2/app/images/logo.png/app/http://backend_server//images/logo.pngForward to http://backend_server/images/logo.png
3/app/app/http://backend_server//Forward to http://backend_server/
4/otherNo matchN/AN/AServe locally or other rules
5N/AN/AN/AN/AEnd of processing
💡 Requests not matching /app/ are handled by other rules or served locally.
Status Tracker
VariableStartAfter 1After 2After 3Final
Client Request URL/app/page1/app/images/logo.png/app/other/other
Location MatchYesYesYesNoNo
proxy_pass Targethttp://backend_server/http://backend_server/http://backend_server/N/AN/A
Rewritten URL/page1/images/logo.png/N/AN/A
Key Moments - 3 Insights
Why does the URL /app/page1 become /page1 when forwarded?
Because proxy_pass with a trailing slash replaces the matching location prefix (/app/) with the slash in proxy_pass (/) as shown in execution_table rows 1 and 2.
What happens if the client requests /app without a trailing slash?
Nginx treats /app as matching /app/ location and rewrites it to /, forwarding to backend root as in execution_table row 3.
Why are requests like /other not forwarded to backend_server?
Because they do not match the location /app/ block, so proxy_pass is not applied, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the rewritten URL for client request /app/images/logo.png?
A/logo.png
B/app/images/logo.png
C/images/logo.png
D/app/
💡 Hint
Check execution_table row 2 under 'Rewritten URL' column.
At which step does the client request not match the /app/ location?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at execution_table row 4 under 'Location Match' column.
If proxy_pass was set without a trailing slash like 'proxy_pass http://backend_server', how would the rewritten URL for /app/page1 change?
A/page1
B/app/page1
C/backend_server/page1
D/
💡 Hint
Recall that without trailing slash proxy_pass appends full original URI after the backend URL.
Concept Snapshot
proxy_pass forwards client requests to another server.
If proxy_pass URL ends with /, Nginx replaces matching location prefix with that /.
If no trailing /, Nginx appends full original URI.
Requests not matching location block are not proxied.
Used to create reverse proxy setups easily.
Full Transcript
The proxy_pass directive in Nginx forwards incoming client requests to a backend server. When a client sends a request matching a location block with proxy_pass, Nginx rewrites the URL by replacing the matching prefix with the proxy_pass URL if it ends with a slash. Then it sends the request to the backend server. The backend processes it and returns a response, which Nginx sends back to the client. Requests not matching the location block are handled by other rules or served locally. This allows Nginx to act as a reverse proxy, forwarding requests transparently.

Practice

(1/5)
1. What is the primary purpose of the proxy_pass directive in an Nginx configuration?
easy
A. To block incoming requests based on IP address
B. To serve static files directly from Nginx
C. To forward client requests to another server
D. To rewrite URLs within the same server

Solution

  1. Step 1: Understand the role of proxy_pass

    The proxy_pass directive is used to send client requests received by Nginx to another server, often called a backend or upstream server.
  2. Step 2: Compare with other options

    Serving static files is done by root or alias, blocking IPs uses deny, and URL rewriting uses rewrite. These are different from proxy_pass.
  3. Final Answer:

    To forward client requests to another server -> Option C
  4. Quick Check:

    proxy_pass forwards requests [OK]
Hint: Remember: proxy_pass sends requests to another server [OK]
Common Mistakes:
  • Confusing proxy_pass with static file serving
  • Thinking proxy_pass blocks requests
  • Mixing proxy_pass with URL rewriting
2. Which of the following is the correct syntax to forward all requests from /app/ to http://backend.local/ using proxy_pass inside a location /app/ block?
easy
A. proxy_pass http://backend.local/;
B. proxy_pass http://backend.local/app/;
C. proxy_pass backend.local;
D. proxy_pass http://backend.local

Solution

  1. Step 1: Understand proxy_pass URL format

    The URL in proxy_pass must include the scheme (http://) and a trailing slash if you want to replace the matching part of the request URI.
  2. Step 2: Analyze options

    proxy_pass http://backend.local/; uses http://backend.local/ with trailing slash, which correctly forwards requests replacing /app/ with /. proxy_pass http://backend.local/app/; appends /app/ again, causing duplication. proxy_pass backend.local; lacks scheme, invalid syntax. proxy_pass http://backend.local misses trailing slash, causing different URI handling.
  3. Final Answer:

    proxy_pass http://backend.local/; -> Option A
  4. Quick Check:

    Correct proxy_pass URL includes scheme and trailing slash [OK]
Hint: Always include scheme and trailing slash for correct URI mapping [OK]
Common Mistakes:
  • Omitting http:// scheme
  • Missing trailing slash causing URI errors
  • Appending location path twice
3. Given this Nginx config snippet:
location /api/ {
    proxy_pass http://backend.local/;
}

If a client requests /api/users, what will be the proxied request URI sent to http://backend.local?
medium
A. /api/users
B. /users
C. /
D. /api/

Solution

  1. Step 1: Understand URI replacement with trailing slash

    When proxy_pass URL ends with a slash, Nginx replaces the matching part of the request URI (/api/) with the path in proxy_pass (which is / here).
  2. Step 2: Apply to example

    Request URI /api/users has /api/ replaced by /, resulting in /users sent to backend.
  3. Final Answer:

    /users -> Option B
  4. Quick Check:

    Trailing slash in proxy_pass replaces matching location path [OK]
Hint: Trailing slash means replace matching URI part [OK]
Common Mistakes:
  • Assuming original URI is sent unchanged
  • Confusing with proxy_pass without trailing slash
  • Thinking /api/ prefix remains in proxied URI
4. You have this Nginx config:
location /service/ {
    proxy_pass http://backend.local/service/;
}

Clients report 404 errors from backend for requests like /service/data. What is the likely cause?
medium
A. proxy_pass URL duplicates the /service/ path causing wrong backend URI
B. Missing trailing slash in proxy_pass URL
C. Backend server is down
D. Nginx location block syntax error

Solution

  1. Step 1: Analyze proxy_pass with matching location

    When proxy_pass URL includes the same path as location, Nginx appends the full request URI after it, causing duplication.
  2. Step 2: Understand effect on backend URI

    Request /service/data becomes /service/service/data at backend, likely causing 404 errors.
  3. Final Answer:

    proxy_pass URL duplicates the /service/ path causing wrong backend URI -> Option A
  4. Quick Check:

    Duplicated path in proxy_pass causes 404 [OK]
Hint: Avoid repeating location path in proxy_pass URL [OK]
Common Mistakes:
  • Ignoring duplicated path in proxy_pass
  • Assuming backend down without checking URI
  • Confusing missing slash with duplicated path
5. You want to forward requests from /app/ to http://backend.local/ but keep the original /app/ prefix in the proxied URI. Which proxy_pass configuration achieves this?
hard
A. proxy_pass http://backend.local/;
B. proxy_pass http://backend.local/app/
C. proxy_pass http://backend.local/app;
D. proxy_pass http://backend.local;

Solution

  1. Step 1: Understand proxy_pass without trailing slash

    If proxy_pass URL has no trailing slash, Nginx appends the full original request URI after the host.
  2. Step 2: Apply to example

    Using proxy_pass http://backend.local; inside location /app/ sends requests like /app/data as http://backend.local/app/data, preserving the prefix.
  3. Final Answer:

    proxy_pass http://backend.local; -> Option D
  4. Quick Check:

    No trailing slash keeps original URI prefix [OK]
Hint: No trailing slash keeps full original URI [OK]
Common Mistakes:
  • Adding trailing slash removes original prefix
  • Appending /app/ twice causing duplication
  • Missing scheme in proxy_pass URL
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