Nginx + Magento configuration problems. PHP files are being downloaded

Nginx + Magento configuration problems. PHP files are being downloaded

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Nginx + Magento configuration problems. PHP files are being downloaded

Solving Nginx + Magento Configuration Problems: PHP Files Are Being Downloaded

Dear Stefan,

I understand your frustration with getting Magento to run smoothly on Nginx. Configuring these two powerful tools can indeed be challenging for beginners. I’ll walk you through the issues in your current setup and provide a step-by-step solution.

The Current Problem

In your current configuration, when navigating to your Magento site at https://mailtest.unitracer.nl/, the server is trying to download PHP files instead of processing them as part of the Magento application. This happens because Nginx does not know how to handle the .php extension properly in this context.

Step 1: Understand the try_files Directive

The key issue lies in your main Magento vhost file, specifically in these lines:

location  / {
    index index.php;
    try_files $uri $uri/ @handler;
}

The try_files directive is a powerful tool that allows Nginx to attempt different file locations until it finds one that matches the request. However, in your case, you’re missing the final parameter needed for this directive to work correctly with Magento.

Step 2: Correcting the try_files Directive

To fix this, we need to add a fourth parameter to the try_files directive:

location  / {
    index index.php;
    try_files $uri $uri/ $uri/index.php?$args @handler;
}

This addition ensures that Nginx will attempt to find the requested file at three different locations before passing it to the @handler block for processing as a PHP file.

Step 3: Simplifying the location ~ .php$ Block

Next, let’s simplify your PHP file handling. You don’t need the separate location ~ .php/ { rewrite ^(.*.php)/ $1 last; } block:

location ~ \.php$ {
    if (!-e $request_filename) {
        return 404;
    }

    # ... existing configuration ...

}

We’ve removed the unnecessary rewrite rule and simplified the block.

Step 4: Removing the @handler Location Block

Your current @handler location block isn’t needed in this context:

location @handler {
    # ... remove entire block ...
}

With our changes, Nginx will automatically handle PHP files correctly within your Magento application.

Step 5: Ensure Your SSL Certificates Are Correctly Configured

While not directly related to the PHP download issue, it’s worth noting that in your main vhost file, you have two different listen directives for port 443:

listen 80;
listen 443 default ssl;

The default keyword is redundant here. Remove it and keep only one listen directive for port 443:

listen 80;
listen 443 ssl;

Ensure that your SSL certificate paths (/etc/nginx/ssl/server.crt and /etc/nginx/ssl/server.key) are correct.

Step 6: Restart Your Nginx Server

After making these changes, restart your Nginx service to apply the new configuration:

sudo systemctl restart nginx

Conclusion

With these modifications, you should now be able to access your Magento site without the server trying to download PHP files. Remember that proper configuration is key to getting the most out of Nginx and Magento.

If you’re interested in learning more about Nginx configurations for e-commerce platforms like Magento, I recommend checking out Laravel Company’s blog. We regularly publish articles on best practices for Nginx setup and optimization.

Keep practicing! With persistent troubleshooting and a willingness to learn, you’ll become an expert in no time.

Indexing Extensions Configuration Email

Our network