Home > Operation and Maintenance > Nginx > How to disable PHP execution in Nginx

How to disable PHP execution in Nginx

WBOY
Release: 2023-05-27 09:13:14
forward
1839 people have browsed it

1. Edit the Nginx configuration file

You need to edit the Nginx configuration file to disable the execution of PHP scripts. Try entering the following command in the terminal, if you don’t know where the Nginx configuration file is

$ locate nginx.conf
Copy after login

Depending on your operating system, the Nginx configuration file 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;
}
Copy after login

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

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

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

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.

The above is the detailed content of How to disable PHP execution 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