Home >Operation and Maintenance >Nginx >How to disable access to .php files in Nginx

How to disable access to .php files in Nginx

WBOY
WBOYforward
2023-05-17 11:16:391923browse

  1. Use the location directive

Use the location directive in the Nginx configuration file to restrict access to specific directories or file access. To prohibit access to .php files, you can add the following code to the location directive

location ~ \.php$ {
    deny all;
}

In the above code, \ represents the escape character, . represents any character, and $ represents the end. Therefore, the function of this code is to use regular expressions to match all files ending with .php and then prohibit them from being accessed.

It should be noted that this method is only suitable for prohibiting access to PHP files and is invalid for other types of files.

  1. Use the if directive

The way to prohibit access to .php files is not only the location directive, but also the if instruction. In the Nginx configuration file, you can add the following code:

if ($request_uri ~* “\.php”) {
    return 403;
}

The meaning of the above code is that when the requested URI contains .php, 403 (Access Denied) is returned.

However, there are some risks in using if instructions, which may lead to security vulnerabilities. Therefore, it is recommended to use if directive only when necessary.

  1. Modify the configuration file of the PHP interpreter

In addition to prohibiting access to .php files in the Nginx configuration file , we can also achieve the same effect by modifying the configuration file of the PHP interpreter.

In the configuration file php.ini of the PHP interpreter, you can add the following code:

security.limit_extensions = .php

The function of this code is to limit only .php files to be executed, and other types of files will Prohibited from execution. In this way, unnecessary security risks can be avoided.

It should be noted that this method is only applicable to prohibiting the execution of php files and is invalid for accessing .php files.

The above is the detailed content of How to disable access to .php files in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete