To prevent Nginx from treating PHP as a static file, you must define a location block that routes .php files to your PHP processor.
Nginx serves .php files as downloads, instead of executing them
Open your site's configuration file (usually in /etc/nginx/sites-available/ ) and ensure the following block is present inside the server { ... } section: nginx prevent download php
If you've added the code above and it still fails, check these common culprits:
location ~ \.php$ { include snippets/fastcgi-php.conf; # Ensure this path matches your installed PHP version (e.g., 8.1, 8.2, 8.3) fastcgi_pass unix:/run/php/php8.2-fpm.sock; } Use code with caution. To prevent Nginx from treating PHP as a
If your browser keeps downloading your scripts, follow this guide to fix the configuration and secure your environment. 1. The Core Fix: Configure FastCGI
When Nginx serves a PHP file as a download instead of executing it, it is usually because the server hasn't been told how to "talk" to a PHP processor. Unlike some other web servers, Nginx does not include a built-in PHP engine; it acts as a reverse proxy that must pass script requests to a separate service like (FastCGI Process Manager). If your browser keeps downloading your scripts, follow
The location ~ \.php$ directive uses regular expressions to catch any request ending in .php . Instead of serving the file, Nginx sends it to the Unix socket specified in fastcgi_pass for execution. 2. Common Reasons for Failure