Current Location: Home> Latest Articles> How to Block PHP Script Execution in Specific Directories on Nginx

How to Block PHP Script Execution in Specific Directories on Nginx

gitbox 2025-07-28

Introduction

In some cases, you may want to prevent PHP scripts from being executed in specific directories on your Nginx server. This can be easily achieved by modifying the Nginx configuration file, thus enhancing your website's security. This article will walk you through how to configure Nginx to block PHP script execution in specific directories.

Principle of Blocking PHP Execution in Specific Directories on Nginx

To block PHP execution in specific directories on Nginx, you can use the `location` directive to set access restrictions for specific directories. More specifically, you'll use regular expressions to match directory paths and then block PHP execution with the `deny all` directive. Below is the basic principle of the configuration:

Creating an Nginx Server Block

First, you need to create a server block in your Nginx configuration file, where you'll configure your website. This server block will include rules for different directories.


server {
    listen 80;
    server_name example.com;
    # Configure other Nginx options
}

Configuring Restrictions to Block PHP Execution

Next, configure the restriction to block PHP execution in the specific directory. You can use the `location` directive to specify the directory and apply the restriction using regular expressions.


server {
    listen 80;
    server_name example.com;
    # Configure other Nginx options

    location ~ /(dir1|dir2)/.*\.php$ {
        deny all;
    }
}

In the configuration above, we use the `location` directive to match requests starting with `/dir1/` or `/dir2/` and ending with `.php`. The `deny all;` directive blocks all matching requests. You can modify the regular expression and matched directories according to your needs.

Complete Configuration Example

Here is a complete configuration example demonstrating how to block PHP script execution in specific directories on Nginx.


server {
    listen 80;
    server_name example.com;

    root /path/to/website;
    index index.html;

    location ~ /(dir1|dir2)/.*\.php$ {
        deny all;
    }

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

In this example, the root directory is set to `/path/to/website`, and the default index file is set to `index.html`. The `location` directive blocks PHP execution in the `/dir1/` and `/dir2/` directories. For other requests, Nginx attempts to serve static files and returns a 404 error if the file is not found.

Conclusion

By using the `location` directive in Nginx, you can effectively block PHP script execution in specific directories. This is a simple and effective way to enhance website security. When implementing this configuration, ensure you set the correct path for directories where PHP execution should be blocked and modify the regular expression as needed. After making changes, remember to reload Nginx to apply the new configuration.