Sub topic 1
How a Request Reaches Your Application
A web request usually passes through several layers before your application produces a response. Understanding this path makes configuration and troubleshooting much easier.
Prerequisites
This chapter assumes you can use a Linux command line, edit files, manage services, and read basic file permissions. You should also understand HTTP requests, responses, status codes, and headers. The examples use Nginx and assume you have a small PHP or Python application.
The request path
A client, such as a browser, resolves a domain name to an IP address and opens a connection to the server. The web server then receives the HTTP request.
Nginx is commonly used as the first application-aware component. It selects a server block using the destination port and the request's Host header. Inside that block, location rules decide what happens next.
A request for a static file, such as /static/site.css, can be handled entirely by Nginx. Nginx reads the file from the configured document root and sends it to the client. The application does not need to run for this request.
A dynamic request needs application code. Nginx passes the request to another process:
- PHP requests usually go to PHP-FPM through FastCGI.
- Python applications usually run in an application server such as Gunicorn.
- Nginx sends Python requests to Gunicorn through a reverse proxy.
The application creates a response. The application server or PHP-FPM returns that response to Nginx, which sends it back to the client.
Choosing the serving model
There are three common models:
| Model | Application connection | Typical use |
|---|---|---|
| Embedded module | Code runs inside the web server process | Older or tightly integrated deployments |
| FastCGI | Nginx sends a structured request to a separate process | PHP with PHP-FPM |
| Reverse proxy | Nginx forwards HTTP requests to another server | Python with Gunicorn or another app server |
The embedded model can be simple, but it couples the application runtime to the web server. FastCGI separates PHP execution from Nginx while using a protocol designed for this purpose. A reverse proxy gives the application server control over its own runtime and is a flexible choice for Python applications.
What Nginx decides
Nginx first chooses the server block, then chooses the best matching location. A typical configuration might serve files from a root directory and send application routes to a backend:
server {
listen 80;
server_name example.com;
root /var/www/example/public;
location /static/ {
try_files $uri =404;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Here, Nginx handles static files directly. Other paths are forwarded to a process listening on port 8000. The forwarded headers preserve details that would otherwise describe only the Nginx connection.
Exercise
A browser requests /static/app.css, and the file exists under the configured root. Which component should normally read and return the file?
Show answer
Nginx should serve it directly without contacting PHP-FPM or Gunicorn.