0
0
Nginxdevops~10 mins

proxy_pass directive in Nginx - Step-by-Step Execution

Choose your learning style9 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.