Search code examples
phpnginxfastcgifpm

Nginx return 404 when executing script


I have the php script in en /var/www/myproyect/public/scripts/myscript.php

I'm using php 7.4 with nginx and php-fpm

The file exists, if I open a txt it opens it without problem, but when it is a .php they return 404

location  ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
 }

Response of curl -I https://myurl/scripts/myscript.php:

HTTP/2 404 server: nginx/1.14.2 date: Tue, 14 Dec 2021 12:18:44 GMT content-type: text/html content-length: 169


Solution

  • I had to specify the folder where the script is located, with this configuration it works correctly

    location ~ /scripts/myscript.php {
       try_files $uri $is_args$args;
       error_page 405 =200 $uri;
       fastcgi_split_path_info ^(.\.php)(/.)$;
       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
    }
    

    Answered by OP in question, removed and added as answser