Sub topic 2
Embedded, FastCGI, and reverse-proxy models
This chapter compares the three ways a web server can run application code. It assumes you can use the Linux command line, edit files on a Debian/Ubuntu-style server, and read HTTP requests, responses, status codes, and headers. Later chapters configure each model in detail. Here you learn the working model, what each one costs, and which errors point to which model.
The question every model answers
A web server such as Nginx or Apache is good at accepting connections, parsing HTTP, and sending files. It does not know how to run your PHP or Python code. Every deployment must answer one question: where does the application code run, and how does the request reach it?
There are three common answers.
- Embedded: the application runtime is loaded inside the web server process.
- FastCGI: the application runs in separate, long-lived worker processes. The web server talks to them over the FastCGI protocol.
- Reverse proxy: the application runs as its own HTTP server. The web server forwards complete HTTP requests to it.
Model 1: embedded runtime
In the embedded model, the web server loads the language runtime as a module. The classic example is Apache with mod_php. Another is Apache with mod_wsgi for Python.
The request path is short. Apache accepts the connection, and the same process runs the PHP code and writes the response. No second hop exists.
This model has real costs:
- Every worker carries the runtime. With Apache's prefork setup, each process loads PHP. That includes processes that only serve an image or a CSS file, so memory use is high.
- The web server and the application share a user. Apache runs your code as its own user, so you cannot easily give each site a separate identity.
- A crash or leak affects the web server. A runaway script can take down a process that also serves unrelated requests.
- You are tied to one web server. Nginx does not load PHP or WSGI modules this way, so this model is mostly an Apache choice.
It still works for a single small site on Apache, and it needs the fewest moving parts. Most new deployments choose one of the other two models.
Model 2: FastCGI
CGI was the original way to run programs from a web server. The server started a new process for each request, and that process printed the response. That is simple but slow, because starting a process per request is expensive.
FastCGI keeps the application processes alive and reuses them. The web server sends each request to a worker over a binary protocol, then reads the response back. For PHP, the standard FastCGI implementation is PHP-FPM (FastCGI Process Manager). It runs one or more pools of worker processes, and each pool can have its own user, limits, and socket.
Nginx cannot run PHP itself, so FastCGI is the normal way to use PHP with Nginx. The relevant parts of an Nginx configuration are small:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}The snippets/fastcgi-php.conf file and the socket path are Debian/Ubuntu conventions. They depend on the distribution and the installed PHP version.
FastCGI differs from plain HTTP in one important way. The web server does not forward the request as text. It passes parts of it as named parameters, such as SCRIPT_FILENAME, QUERY_STRING, and REQUEST_METHOD. PHP-FPM uses SCRIPT_FILENAME to decide which file to execute, so the web server must tell it the correct path.
This has a practical consequence. FastCGI only works if both sides agree on file paths. Nginx and PHP-FPM must see the same filesystem layout. That is easy on one host and a common source of trouble when they run in separate containers.
Key properties of the FastCGI model:
- Static files stay with Nginx. Only matching requests, usually
.php, reach PHP. - PHP workers are separate from the web server. You can restart or resize them without touching Nginx.
- Each pool can run as a different user, which supports per-site isolation.
- The application has no HTTP server of its own. You cannot test it with
curlon a port in the same way.
Model 3: reverse proxy
In the reverse-proxy model, the application runs as a full HTTP server on a local socket or port. The web server accepts the client connection and makes a second HTTP request to the application. It then relays the response.
This is the standard model for Python. A WSGI or ASGI application does not speak HTTP directly. An application server such as Gunicorn does, and it calls your code through the WSGI or ASGI interface. The same model fits Node.js, Ruby, Go, and Java, and it also works for PHP with an application server like RoadRunner or FrankenPHP.
Because the application speaks HTTP, you can test it before the web server exists:
gunicorn --workers 3 --bind 127.0.0.1:8000 app:application
curl -i http://127.0.0.1:8000/Then Nginx forwards to it:
location / {
proxy_pass http://unix:/run/gunicorn/app.sock;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}The reverse-proxy model has a side effect that the other models avoid. The application sees the connection from Nginx, not from the real client. Without extra headers, the app sees the proxy's address as the client IP, and it sees plain HTTP even when the browser used HTTPS. You fix this with forwarded headers, covered in a later chapter.
Reverse proxying also adds a useful buffering behavior. Nginx reads the whole response from a fast application worker and then sends it to a slow client at the client's pace. This frees the worker sooner. A slow mobile connection no longer ties up a Gunicorn worker.
Comparing the models
| Aspect | Embedded | FastCGI | Reverse proxy |
|---|---|---|---|
| Where code runs | Inside the web server process | Separate worker pool | Separate application server |
| Protocol to the app | Function calls in-process | FastCGI | HTTP |
| Typical stack | Apache with `mod_php` | Nginx with PHP-FPM | Nginx with Gunicorn |
| Per-site user isolation | Weak | Strong (per pool) | Strong (per service) |
| Restart app without web server | No | Yes | Yes |
| Test app directly with `curl` | Through the web server only | Awkward | Easy |
| App sees real client IP | Yes | Yes, via FastCGI parameters | Only through forwarded headers |
| Extra process to manage | None | PHP-FPM service | App server service |
Choosing a model
Use these criteria rather than habit.
- Start from the language. PHP on Nginx means FastCGI with PHP-FPM. Python means a reverse proxy to a WSGI or ASGI server. These are the paths with the best documentation and tooling.
- Check your web server. If you already run Apache for
.htaccessrules and have one simple PHP site,mod_phpis acceptable. Apache can also use PHP-FPM throughproxy_fcgi, which gives you the benefits of the FastCGI model. - Consider isolation. If several sites share a host, you want separate users and limits per site. FastCGI pools and separate application services give you that. An embedded runtime does not.
- Consider who supervises the app. PHP-FPM ships with a service unit. A Gunicorn process does not, so you must write a systemd unit for it. That is extra work, and it is also what gives you restart-on-failure and boot-time startup.
- Consider concurrency style. Long-lived connections, such as WebSockets, need an ASGI server behind a reverse proxy. FastCGI suits short request and response cycles.
How the models fail
Each model produces recognizable errors. Knowing which one you are in narrows the search quickly.
- Embedded: failures usually appear as 500 errors in the Apache error log. Memory exhaustion from too many heavy workers also happens.
- FastCGI: a 502 Bad Gateway means Nginx could not reach PHP-FPM. Typical causes are a stopped service, a wrong socket path after a PHP upgrade, or a socket the Nginx user cannot access. A blank page or "File not found." message often means
SCRIPT_FILENAMEpoints to a file that does not exist. - Reverse proxy: a 502 means the application server is down or the address is wrong. A 504 Gateway Timeout means it accepted the request but did not answer in time. A redirect loop after enabling HTTPS usually means the application cannot tell the original scheme and needs
X-Forwarded-Proto.
Verifying which model is in use
You can see the model on a running server. These commands show the processes and their sockets.
ps -eo user,pid,cmd | grep -E 'nginx|apache|php-fpm|gunicorn' | grep -v grep
sudo ss -xlp | grep -E 'php|gunicorn'If you see php-fpm: pool www processes and a Unix socket, the server uses FastCGI. If you see gunicorn workers listening on a socket or port, it is a reverse proxy. If Apache processes are the only ones involved and the PHP module is loaded, it is embedded. You can confirm the last case with apachectl -M | grep php.
Exercise
A Python app runs under Gunicorn on a Unix socket. Nginx returns 502 for every request, but curl against the socket from the same machine works. Which model is in use, and what should you check first?
Show answer
This is the reverse-proxy model. Since the application answers when you call it directly, the application is healthy. Check whether the Nginx worker user can access the socket: its path in proxy_pass must match the one Gunicorn binds to, and the user needs execute permission on every directory in the path and write permission on the socket file. Then check /var/log/nginx/error.log for a "connect() failed" message, which names the exact reason.
Exercise
Why does the FastCGI model require Nginx and PHP-FPM to agree on file paths, while the reverse-proxy model does not?
Show answer
In FastCGI, Nginx sends a SCRIPT_FILENAME parameter and PHP-FPM opens that file on its own filesystem. If the path does not exist for PHP-FPM, the request fails. In the reverse-proxy model, Nginx sends only an HTTP request with a URL. The application server maps it to code internally, so the web server never needs to know where the code lives.