3

I'm writing a web application using Slim 3, and am having difficulty getting nginx to play nicely with one of my routes.

My application allows users to upload files and view them, simple enough. This generates URLs like /viewfile/{user_id}/{filename}. When the filename ends in .php, I would expect nginx's try_files to detect if it exists before attempting to pass it on to php-fpm. However, I suppose the php location block is still trying to serve it as a valid PHP file, as I keep getting php-fpm's No input file specified. error when attempting to view an uploaded php file.

Is there any way to get nginx to not attempt to serve all PHP file using php-fpm and instead pass it on to index.php like the rest of the nonexistent URLs do, but still maintain the ability for only existing PHP files to be served?

Here is my config, trimmed down to the relevant parts, if it's necessary:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    root /var/www/html/public;
    index index.php index.html index.htm;

    server_name {redacted};

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

    location ~ \.php$ {
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php-fpm/php7.0-fpm.sock;
        include fastcgi_params;
    }
}
Jesse Nickles
  • 250
  • 1
  • 12
Bytewave
  • 131
  • 4
  • Does the "check if files exists" section of this site help? https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ – Tim Dec 11 '16 at 21:56
  • 1
    https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#passing-uncontrolled-requests-to-php You need the third option. – Michael Hampton Dec 11 '16 at 21:57
  • 1
    @MichaelHampton It seems that adding a `try_files` block to the PHP `location` block worked. Is that what you were suggesting? – Bytewave Dec 11 '16 at 22:21
  • Precisely that! – Michael Hampton Dec 11 '16 at 23:16
  • Alright. I'll have to figure out how to apply that to my local environment (Laravel Homestead), but otherwise it works. Thank you! I'd +1 you if it wasn't a comment. – Bytewave Dec 11 '16 at 23:17
  • Related: https://serverfault.com/questions/806391/nginx-wordpress-404-pages-all-works-but-php-files – Jesse Nickles Sep 10 '22 at 19:12

0 Answers0