Nginx: Resolving “No input file specified” error


If you are using nginx with php-cgi and have followed the standard procedure to set it up, you might often get the “No input file specified.” error. What happens here is that, when nginx receives the request to serve a non-existent php file, it passes the request to php-cgi. Php-cgi, while trying to processs the request, finds that the php file does not exist at all. Hence it sends a “No input file specified.” message with a “404 Not Found” header.

Although, it’s not technically an error, we can catch the request for the non-existent php file and show a custom 404 page.

First, find out the version of nginx you are using.

[code language=”bash”]nginx -V[/code]

You’ll get an output like this :

[code language=”bash”]nginx version: nginx/0.7.65
TLS SNI support enabled
configure arguments: –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –pid-path=/var/run/nginx.pid –lock-path=/var/lock/nginx.lock –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/body –http-proxy-temp-path=/var/lib/nginx/proxy –http-fastcgi-temp-path=/var/lib/nginx/fastcgi –with-debug –with-http_stub_status_module –with-http_flv_module –with-http_ssl_module –with-http_dav_module –with-http_gzip_static_module –with-http_realip_module –with-mail –with-mail_ssl_module –with-ipv6 –add-module=/build/buildd-nginx_0.7.65-2~bpo50+1-i386-1taiV6/nginx-0.7.65/modules/nginx-upstream-fair[/code]

If php-cgi is running on port 9000, you’ll have something like this in your vhosts file :

[code]location ~ .php$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME ….
……………………………..
……………………………..
}[/code]

Since nginx versions less than 0.6.36 doesn’t have the try_files directive, we’ll have two versions of the code.

For nginx 0.6.36+
We’ll use try_files here to catch and display an error page.

[code]location ~ .php$ {
try_files $uri /path/to/404.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ….
……………………………..
……………………………..
}[/code]

For older versions :

[code language=”bash”]location ~ .php$ {
fastcgi_intercept_errors on;
error_page 404 /path/to/404.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME ….
……………………………..
……………………………..
}[/code]

This will display the chosen 404 page for non-existent php files instead of the unhelpful “No input file specified.” message.