I installed magento2 with LEMP (nginx + php8.1-fpm)

magento root: /var/www/html/

I am trying to add a seperate folder and execute a phpfile inside this folder in magento root:


but when i try to open https://example.com/myfolder/connect.php

i am having : 404 error: Page not found.

what should i do?

2 Answers 2


You have to create your folder inside pub folder to access the link as you expected: /var/www/html/pub/mynewfolder

If it does not work, edit nginx.conf or nginx.conf.sample depend on which file you are using for config nginx to add the following rule:

# PHP entry point for custom scripts
location /mynewfolder/ {
    try_files $uri =404;
    fastcgi_pass   fastcgi_backend;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

    fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
    fastcgi_param  PHP_VALUE "memory_limit=756M \n max_execution_time=18000";
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;

    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;

And restart nginx

  • If this answer helps you resolve your issue, please mark it as accepted (the check symbol at the top-left corner of my answer). That indicates your issue is solved and helps others with the same issue find out the verified answer easily.
    – Tu Van
    Dec 9, 2022 at 18:17
  • it doesnt work. Also the folder have to be in magento root; -/var/www/html/mynewfolder -example.com/mynewfolder/connect.php
    – Gkna
    Dec 9, 2022 at 18:26
  • When you create your folder with the path I mentioned you can go example.com/mynewfolder/connect.php
    – Tu Van
    Dec 9, 2022 at 18:32
  • it doesnt work...
    – Gkna
    Dec 9, 2022 at 18:36
  • @Gkna I've updated the solution in my answer, please give it a try.
    – Tu Van
    Dec 9, 2022 at 18:43

The solution is :

location /mycustomfile.php {
    try_files $uri $uri/;
    root /var/www/html;
    location ~* \.php$ {
    fastcgi_pass fastcgi_backend;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.