1

I am facing the same dreaded "No input file specified." message in NGINX. A brief description of what I am trying to achieve is as given below.

  1. Any existing URL ending with ".php" extension should show as 404 error [working]
  2. Any existing URL without ".php" extension will execute the file [working]
  3. Any non-existent URL should throw 404 error [not working]. In this case the error thrown by NGINX is "No input file specified."

I have visited and tried to implement the solution located at Nginx + php fastcgi showing "No input file specified." instead of 404 but this did not solve my problem.

I am really not sure how to render 404 page in case of point 3.

Contents of /etc/nginx/sites-enabled/default file is given below

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html/example.com;
    index index.php index.html index.htm;
    server_name example.com;
    error_page 404 /error/custom_404;

    location / {
        try_files $uri $uri/  @missing;
    }

    location ~ (\.php$|myadmin) {
    return 404;
    }

    location @missing {
            fastcgi_param SCRIPT_FILENAME "$document_root$uri.php";
            fastcgi_param PATH_TRANSLATED "$document_root$uri.php";
            fastcgi_param QUERY_STRING    $args;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Please keep in mind that I am just a beginner and not an expert. Any help is appreciated.

1 Answer 1

1

Your location @missing block does nothing to verify the existence of the file before passing it to php-fpm. Add a check for file existence:

location @missing {
    if (!-f $document_root$uri.php) { return 404; }
    ...
}

See this document for more, and this caution on the use of if.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding try_files $uri.php =404; in the missing block throws 404 error for all the requests which were earlier served. try_files is present in another block above it.
Sorry my bad, try_files changes the URI. Answer updated to use an if statement instead.
Thanks a lot @RichardSmith. This was what I needed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.