skip to Main Content

I am trying to let a custom script which is in the root directory of the Magento 2 installation but when I navigate to it on my browser, it returns a 404 error. For testing I tried to create an empty php file (abc.php) then navigate to it in the browser (domain/abc.php) it will still return a 404 even though I am certain the file exists in the location. I have tried CHMODding to 777 and changing ownership of the file to the same as the rest of the directory but I still get this 404.

I am using ubuntu 18.04 + NGINX as web server to run Magento 2. The Magento 2 installation is working on the browser, just any custom script I navigate to returns a 404. Has anyone ever had anything similar or have an idea on what to do next to debug this problem :/?

2

Answers


  1. Try This if Inside of domain:-

    localhost/domailname/filename.php
    

    Try This if File outside of domain

    localhost/filename.php
    
    Login or Signup to reply.
  2. If you’ve setup NGINX according to Magento 2 recommendations, then it will only allow PHP execution from a list of well defined, known PHP files. Namely, if you look here, you’ll take notice of:

    location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check).php$ {
        ...
        fastcgi_pass   fastcgi_backend;
        ...
    }
    

    And this:

    location ~* (.php$|.htaccess$|.git) {
        deny all;
    }
    

    Which essentially means to deny execution for any PHP file other than /index.php, /get.php, etc.

    So to allow for execution of a custom PHP file, you will need to adjust REGEX in the first location, to include the name of the custom file, e.g.:

    location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|test).php$ {
    

    The above would allow execution of /test.php in addition to other known PHP entrypoints.

    Furthermore, if your Magento website’s root in NGINX configuration is /path/to/pub (again, as per recommendations), then you need to place your custom file within that pub subdirectory, and not inside actual root of Magento.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search