Home >Backend Development >PHP Problem >Let's talk about how to disable PHP execution in Nginx

Let's talk about how to disable PHP execution in Nginx

PHPz
PHPzOriginal
2023-04-12 18:54:54899browse

In web servers, Nginx and PHP are very common technologies. Nginx is a high-performance HTTP and reverse proxy server, while PHP is a common server-side scripting language used to dynamically generate web content. In some cases, you may want to disable Nginx from executing PHP scripts. This article will introduce how to disable PHP execution in Nginx.

1. Edit the Nginx configuration file

You need to edit the Nginx configuration file to disable the execution of PHP scripts. If you don't know where Nginx's configuration files are, you can try running the following command in the terminal:

$ locate nginx.conf

Depending on your operating system, Nginx's configuration files may be located in different locations.

Edit Nginx's configuration file and find a line similar to the following:

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

This block defines how Nginx handles PHP scripts. Therefore, we need to disable this block to disable Nginx from executing PHP scripts. You can comment out the entire block like this:

#location ~ \.php$ {
#  try_files $uri =404;
#  fastcgi_pass unix:/var/run/php5-fpm.sock;
#  fastcgi_index index.php;
#  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#  include fastcgi_params;
#}

Save changes and exit the editor.

2. Reload Nginx

Now you need to reload Nginx for the changes to take effect. You can use the command from your system's init script like this:

$ sudo service nginx reload

This will reload Nginx and apply the new configuration file to the server.

3. Test prohibiting PHP execution

Now, you can test whether prohibiting PHP scripts takes effect. To do this, you can try to access a PHP script on your web server, for example:

http://your-server.com/test.php

If everything is working fine, you should see a 404 error page telling you that the page does not exist.

This completes the task of prohibiting Nginx from executing PHP scripts.

Summary

Disabling Nginx from executing PHP scripts is a relatively simple task. You only need to comment out the block that processes PHP scripts in the Nginx configuration file. Then, reload Nginx for the changes to take effect.

The above is the detailed content of Let's talk about how to disable PHP execution in Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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