Sub topic 2
Embedded, FastCGI, and Reverse-Proxy Models
Assume you know basic Linux commands, file permissions, HTTP status codes, and how to edit a Debian or Ubuntu server. You should also have a small PHP or Python application to test. This chapter explains how Nginx passes a request to application code.
The three serving models
A web server can run application code in its own process, communicate with a separate application process using FastCGI, or forward the request to another server over HTTP.
| Model | Application process | Common example | Best fit |
|---|---|---|---|
| Embedded module | Inside the web server | Apache with `mod_php` | Simple PHP deployments |
| FastCGI | Separate managed workers | Nginx with PHP-FPM | PHP applications behind Nginx |
| Reverse proxy | Separate HTTP application server | Nginx with Gunicorn | Python WSGI or ASGI applications |
The request path usually looks like this:
Embedded modules
With an embedded module, the web server loads the application runtime into its worker processes. For example, Apache can load PHP through mod_php.
This model has a short path from request to PHP code. It can be easy to install and understand for a small application. However, the web server process now contains application runtime code. A runtime failure can affect the server worker, and changing application runtime settings may require restarting the web server. Embedded PHP also depends on Apache’s process model, so it is not the usual choice when Nginx is the front end.
FastCGI
FastCGI is a protocol for sending requests to a long-running application process. PHP-FPM, the PHP FastCGI Process Manager, creates and manages PHP worker processes. Nginx sends PHP requests to those workers instead of executing PHP itself.
This separates static file handling from PHP execution. PHP-FPM can use its own user, process limits, logging, and pool settings. Nginx commonly connects to PHP-FPM through a Unix domain socket such as /run/php/php8.3-fpm.sock, or through a TCP address such as 127.0.0.1:9000.
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}The socket path depends on the installed PHP version and distribution. A wrong path usually produces a 502 Bad Gateway response.
Reverse proxy
A reverse proxy accepts the public HTTP request and sends it to an application server. Gunicorn can run a Python WSGI application, while another server might run an ASGI application. Nginx handles the public connection, static files, TLS, and buffering. Gunicorn handles Python application code.
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}The application server should normally listen only on localhost or a Unix socket. Exposing it directly to the internet bypasses Nginx’s access rules and TLS configuration.
Choosing a model
Use an embedded module for a small, conventional Apache and PHP installation when simplicity matters most. Use FastCGI with PHP-FPM when Nginx serves PHP or when PHP worker pools need independent tuning. Use a reverse proxy for Python applications and for any application that already provides an HTTP server.
In every model, the web server remains the public boundary. The application process should run with limited permissions, listen on a private socket or address, and write useful error logs.
Exercise
Which model should normally sit behind Nginx for a Python WSGI application, and why?
Show answer
A reverse proxy model should be used. Nginx forwards requests to Gunicorn over HTTP or a Unix socket, while Gunicorn runs the WSGI application.