Skip to content
Advertisement

Nginx Php-fpm 7.3 Can’t read PHP files from a particular folder

We have a Magento 2 website. For some reason our Nginx/PHP-FPM is unable to read files from MAGEROOT/pub/ folder other than index.php.

We are getting the following error in Nginx Log "Unable to open primary script: /home/goodprice/public_html/releases/current/pub/get.php (No such file or directory)" and the browser shows No input file specified.

Here is the partial Nginx config file.

# Run Magento (behind Varnish)
server {
    listen 8088;

    server_name {{website name}}.com.au www.{{website name}}.com.au m2.{{website name}}.com.au;

    set $MAGE_ROOT /home/goodprice/public_html/releases/current;

    index index.php;
    root $MAGE_ROOT/pub;
    set $code default;

    location /sitemap.xml {
        root $MAGE_ROOT/pub/media;
        autoindex off;
    }

    # Rewrites for edm
    include /etc/nginx/global/rewrites.conf;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # Serve media under /pub/media/
    location /pub/ {
        location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*.xml) {
            deny all;
        }
        alias $MAGE_ROOT/pub/;
        add_header X-Frame-Options "SAMEORIGIN";
    }

    # Rewrite signed static files
    rewrite ^/static/(versiond*/)?(.*)$ /static/$2 last;

    # Static assets
    location ~ ^/static/(versiond*/)?(.*)$ {
        tcp_nodelay on;

        # Images, CSS, JS
        location ~* .(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @static;
        }

        # Fonts
        location ~* .(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @static;
        }

        # Catch all
        try_files $uri $uri/ @static;
    }

    # Media assets
    location /media/ {
        tcp_nodelay on;
        autoindex off;

        # Images, CSS, JS
        location ~* .(jpg|jpeg|png|gif|svg|js|css|ico|txt)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @media;
        }

        # Fonts
        location ~* .(swf|eot|ttf|otf|woff|woff2)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header ETag "";
                add_header Access-Control-Allow-Origin "*";
                add_header Cache-Control "public";
                try_files $uri $uri/ @media;
        }

        # Catch all
        try_files $uri $uri/ @media;
    }

    # Password paths
    location /media/order_attachments {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    location /media/convert {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    # Below prescriptions dir does not contain actual prescriptions
    #location /media/prescriptions {
    #    auth_basic "Restricted";
    #    auth_basic_user_file /etc/nginx/htpasswd;
    #}
    location /media/webforms {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }
    location /media/raveinfosys/exporter {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/htpasswd;
    }

    location @static { rewrite /static/(versiond*/)?(.*)$ /static.php?resource=$2 last; }
    location @media { try_files $uri $uri/ /get.php$is_args$args; }

    # PHP entry point for setup application
    location ~* ^/setup($|/) {
        root $MAGE_ROOT;
        location ~ ^/setup/index.php {
            fastcgi_pass fastcgi_backend;

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

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

        location ~ ^/setup/(?!pub/). {
            deny all;
        }

        location ~ ^/setup/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }

    # PHP entry point for update application
    location ~* ^/update($|/) {
        root $MAGE_ROOT;

        location ~ ^/update/index.php {
            fastcgi_split_path_info ^(/update/index.php)(/.+)$;
            fastcgi_pass fastcgi_backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            include        fastcgi_params;
        }

        # Deny everything but index.php
        location ~ ^/update/(?!pub/). {
            deny all;
        }

        location ~ ^/update/pub/ {
            add_header X-Frame-Options "SAMEORIGIN";
        }
    }

    # Main PHP
    location ~ (index|get|static|report|404|503|health_check|deploy_clear_opcache).php$ {
        try_files $uri =404;

        fastcgi_pass fastcgi_backend;

        fastcgi_param  PHP_FLAG      "session.auto_start=off n suhosin.session.cryptua=off";
        fastcgi_read_timeout         300s;
        fastcgi_connect_timeout      300s;

        # fastcgi_param  MAGE_MODE     $MAGE_MODE;
        fastcgi_param  MAGE_RUN_CODE $code;
        fastcgi_param  MAGE_RUN_TYPE store;

        # Increase fastcgi buffer size to stop nginx errors on large posts
        fastcgi_buffers 32 256k;
        fastcgi_buffer_size 512k;

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

        fastcgi_hide_header  'X-Powered-By';
    }

    # Return 503 if the maintenance flag is found
#    if (-f $MAGE_ROOT/var/.maintenance.flag) {
#        return 503;
#    }
#
#    # Custom 503 error page
#    error_page 503 @maintenance;
#
#    location @maintenance {
#        root /home/goodprice/public_html/maintenance;
#        rewrite ^(.*)$ /503.html break;
#    }

    # Use Magento 403 404 page
    error_page 403 404 /errors/404.php;

   # Banned locations (only reached if the earlier PHP entry point regexes don't match)
    location ~* (.php$|.htaccess$|.git) {
    deny all;
    }
}

This causes a few problems. One is Magento 2 can’t serve the place holder image as it need to execute get.php. It’s not a permission issue as index.php is being executed. Can anybody help fix the problem in the above mentioned Nginx config? Any help will much appreciated.

ls -la from pub dir following

drwxr-xr-x  6 goodprice goodprice 4096 Nov 24 16:16 .
drwxr-xr-x 16 goodprice goodprice 4096 Nov 30 12:11 ..
-rw-rw-r--  1 goodprice goodprice 1038 Nov 11 01:12 cron.php
-rwxrwxr-x  1 goodprice goodprice  102 Nov 10 23:04 deploy_clear_opcache.php
drwxrwxr-x  3 goodprice goodprice 4096 Nov 11 01:12 errors
-rw-rw-r--  1 goodprice goodprice 2775 Nov 24 16:16 get.php
-rw-rw-r--  1 goodprice goodprice 3329 Nov 11 01:12 health_check.php
-rw-rw-r--  1 goodprice goodprice 6206 Nov 11 01:12 .htaccess
-rw-r--r--  1 goodprice goodprice 1360 Nov 12 11:49 index.php
-rw-rw-r--  1 goodprice goodprice  169 Jan 10  2021 info.php
drwxrwxr-x 67 goodprice goodprice 4096 Nov 29 00:01 media
drwxrwxr-x  3 goodprice goodprice 4096 Nov 11 01:12 opt
drwxr-xr-x  4 goodprice goodprice 4096 Nov 30 13:12 static
-rw-rw-r--  1 goodprice goodprice  445 Nov 11 01:12 static.php
-rw-rw-r--  1 goodprice goodprice  101 Nov 11 01:12 .user.ini

Php Fpm conf.d file extract users and groups.

group = "goodprice"
listen.group = "nobody"
listen.mode = 0660
listen.owner = "goodprice"
user = "goodprice"

nginx.conf as following

include /etc/nginx/conf.d/modules/*.conf;

user nobody;

worker_processes  1;
worker_rlimit_nofile 16384;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/conf.d-custom/*.conf;
}

Advertisement

Answer

The issue here was php-fpm config. My mistake in asking the question was that I should have posted the whole php-fpm config with nginx config.

On our server the php-fpm settings are controlled from cpanel for each site. The problem was that php-fpm had php_value[doc_root] set to a folder above the pub folder. That was due to the fact the server and cpanel is configured to have code in /home/goodprice/public_html/. Which I modified to php_value[doc_root] = "/home/goodprice/public_html/releases/current/" thinking that’s Magento root so php should be reading there. But actually it should have been php_value[doc_root] = "/home/goodprice/public_html/releases/current/pub/". So on run time php is looking for a file, but the root (folder) is wrong where it’s looking at. This problem was confusing because php was erroring out with the path of the file but not where it was actually trying to locate the file. I Can’t explain why it would give out a correct path then look for just the file in it’s root folder.

So to summarise, if there is try_files in nginx config make sure that the nginx root, or the final path is same as folder php_value[doc_root] in php-fpm conf. Or better don’t have php_value[doc_root] in your php-fpm at all.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement