Magento powers 12% of all the e-commerce stores all over the world. But, the official support for the Magento 1.x versions will stop after 2020.
Therefore, if you are still on Magento 1, consider migrating to Magento 2.
Due to one of the security features of Magento 2, you cannot run the external PHP files by default.
The file names need to be added in Nginx configuration to execute or we can allow all external files to run but it can break the security of Magento 2.
Steps to run custom PHP files in Magento 2 in NGINX are:-
1. You need to find the included Nginx Sample file in the main Virtual Host Configuration. The name could be different like magento2.conf or also should be in an included Directory.

2. Find the PHP location block (# PHP entry point for the main application) as shown in the below screenshot and add the custom PHP file.
location ~^/(index|get|static|errors/report|errors/404|errors/503|health_check|test).php$
If we want to allow all the custom PHP files to run then replace the above line as-
location ~* .php$ {
For example- If we need to add the test.php then add the test after health_check:-

3. The default Document root for Magento 2 in Nginx is “<Document Root>/pub”. We need to put the external files in the pub directory to execute.
4. So as in this example move the test.php to “<Document Root>/pub”.
We hope you will now be able to run custom PHP files in Magento 2. If not, then our Magento 2 experts are here to help you. ServerGuy also offers Magento hosting services for a secure and high performing store.
You can also leave a comment below if you any other query, we will definitely get back to you.
Also Read:
1 thought on “How To Run Custom PHP Files in Magento 2? (In NGINX)”
often I also facing the same error when the website has error about establishing connection error. Now using this method, everything is working fine. Thanks for your help and subscribed your blog..