Nginx talking to remote php-fpm backend server: No input file specified.

Written by - 2 comments

Published on - Listed in PHP Linux Nginx


Sometimes you get a painful reminder, that you've forgotten the basics.

In a Nginx/PHP-FPM setup I wanted to add a new virtual host with a separate document root:

server {
  listen 80;
  server_name infra;
  error_page   500 502 503 504  /50x.html;
  access_log /var/log/nginx/default.access.log;
  error_log /var/log/nginx/default.error.log;
  root /var/www/default;
  index index.php;
  location = /50x.html {
      root   /usr/share/nginx/html;
  }
  location / {
    try_files $uri $uri/ /index.php;
  }
  # Pass PHP scripts to PHP-FPM
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass phpbackend:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }
}

All PHP requests are sent via the fastcgi_pass to an upstream server called "phpbackend". Looks simple enough and should work. But it didnt.

A manual check on the Host name "infra" with curl revealed that the php file info.php (which exists very well in the /var/www/default folder) was not shown/executed in php-fpm:

curl -H "Host: infra" http://mynginxhost.example.com/info.php
No input file specified.

The nginx error log showed the following entry:

2015/06/10 14:47:43 [error] 32148#0: *2302858 FastCGI sent in stderr: "Unable to open primary script: /var/www/default//info.php (No such file or directory)" while reading response header from upstream, client: 172.20.41.180, server: infra, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://phpbackend:9000", host: "infra"

But as soon as I changed the document root path from "/var/www/default" to "/var/www/phpmyadmin", the info.php (which also exists in the phpmyadmin folder) was viewable. 

I suspected a problem with the file permission, but these were all ok (meaning readable by nginx):

[root@nginx ~]# ll /var/www/
drwxr-xr-x   2 user user 4096  9. Jun 11:11 default
drwxrwxr-x. 10 user  user  4096 19. Mär 11:28 phpmyadmin
drwxrwxr-x.  2 user  user  4096 16. Apr 21:18 util

[root@nginx ~]# ll /var/www/default/
-rw-r--r-- 1 user user 12  5. Jun 16:07 index.html
-rwxrwxr-x 1 user  user  20 19. Mär 11:28 info.php

While I was focusing solely on the nginx server and tried a different set of fastcgi parameters, I finally received an important hint.
I completely forgot that the php files must exist on the php backend (upstream) server!

The file structure on the php backend server revealed, that the default folder (and its files) are missing:

[root@phpbackend ~]# ll /var/www/
drwxrwxr-x. 10 user user 4096 Mar 19 11:27 phpmyadmin
drwxrwxr-x.  2 user user 4096 Apr 16 21:18 util

So the default folder and its php script is missing, from there comes the error Unable to open primary script: /var/www/default//info.php (No such file or directory).
On the backend php server I created the missing folder and the php file:

[root@phpbackend ~]# mkdir -p /var/www/default
[root@phpbackend ~]# echo "" /var/www/default/info.php
[root@phpbackend ~]# chown -R user:user /var/www/default

And now the request worked.

curl -H "Host: infra" http://teaserlb001.nzz.ch/info.php
[...]
PHP Version 5.4.16
[...]

This will be stuck in my mind now for a couple of years - until I forget it again.


Add a comment

Show form to leave a comment

Comments (newest first)

ck from St. Gallen, Switzerland wrote on Oct 2nd, 2015:

Hi Amilla. If you find a way, please let me know. It would make sense of course to only have some "empty php-fpm worker" machines while the php code itself resides on the webserver. The last time I looked it up, I have found no config which would allow such a setup.


Amila from SL wrote on Oct 2nd, 2015:

isn't there a way to do this? I mean keep files on Nginx Server and not on php-fpm server.
Because I am planning to put few php-fpm servers and load balance using nginx server. So it would be better If I can keep files on nginx server instead of coping them to each php-fpm server?
Thanks.


RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder