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

How to disable access to .php files in Nginx

WBOY
Release: 2023-05-17 11:16:39
forward
1953 people have browsed it

  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;
}
Copy after login

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;
}
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template