Hosting & Deployment

Configure Nginx as a Reverse Proxy for PHP Applications

Understand request forwarding, FastCGI boundaries, trusted headers, TLS termination and safe proxy defaults for PHP deployments.

Nginx commonly serves static files and forwards dynamic PHP requests to PHP-FPM. In a reverse-proxy architecture, it may also forward requests to an application server or an upstream service. The key design goal is to make the trust boundary explicit: only trusted proxies should be allowed to supply client-address and scheme headers.

Separate static and dynamic handling

Serve static assets directly from Nginx with deliberate cache headers. Route PHP scripts to PHP-FPM through a narrowly scoped location block. Avoid broad script execution rules that could cause uploaded or unintended files to be interpreted as PHP. Set the document root to the public directory, not the project root.

Configure FastCGI carefully

Pass the correct script filename and request parameters to PHP-FPM. Ensure the configured socket or TCP endpoint matches the PHP-FPM pool. Restrict which paths can reach the PHP handler and return a not-found response for missing scripts rather than forwarding arbitrary paths.

Forwarded headers and client IPs

When Nginx sits behind a load balancer, configure trusted proxy ranges explicitly. Do not trust arbitrary X-Forwarded-For or X-Forwarded-Proto values from the public internet. Incorrect trust settings can undermine IP-based security controls and HTTPS detection.

Validate before reload

Run the Nginx configuration test before applying changes. Keep a known-good configuration and have a rollback path. Monitor error logs, upstream response times and PHP-FPM saturation after deployment.

Checklist

  • Expose only the public document root.
  • Limit PHP execution to intended scripts.
  • Trust forwarded headers only from known proxies.
  • Test configuration before reload.
  • Monitor upstream errors and PHP-FPM capacity.
Share this page Help other developers discover this resource.