Skip to content

How to Configure Nginx with PHP-FPM via fastcgi_pass

DodaTech Updated 2026-06-24 1 min read

Nginx with PHP-FPM via fastcgi_pass serves PHP applications efficiently. Unix sockets and correct SCRIPT_FILENAME are key. This guide walks through the specific troubleshooting steps to diagnose and resolve PHP-FPM configuration issues.

Before You Begin

Before you begin, be sure to have the following in place:

  • A Linux server with the relevant software installed
  • Access to the command line interface
  • Appropriate permissions (root or sudo)

Quick Fix

Wrong

location ~ \.php$ {\n    fastcgi_pass 127.0.0.1:9000;\n}

Wrong: TCP connection to PHP-FPM without SCRIPT_FILENAME

location ~ \.php$ {\n    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n    include fastcgi_params;\n}

Right: Unix socket with SCRIPT_FILENAME parameter

Output

PHP-FPM configured via Unix socket\nFastCGI params: SCRIPT_FILENAME set correctly\nLocation: ~ \.php$

Prevention

To avoid future issues, follow these best practices:

  • Use Unix sockets instead of TCP for better performance
  • Always set SCRIPT_FILENAME to the correct file path
  • Include fastcgi_params for standard FastCGI parameters
  • Use try_files to verify PHP files exist before passing them
  • Set fastcgi_index for directory index PHP files

DodaTech Tools

For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.

Common Mistakes with fastcgi pass

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world NGINX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Why is SCRIPT_FILENAME important for PHP-FPM?|||PHP-FPM needs SCRIPT_FILENAME to know which PHP file to execute. Without it, PHP returns File not found. or a blank page.
Should I use TCP or Unix socket for PHP-FPM? Unix sockets are faster and more secure (no network exposure). Use TCP only if PHP-FPM is on a different server.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro