0

I want to work simple url like http://host/test.php, note there are no trailing / at the end.

I start from standard config from nginx site + serverfault:

 location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^((?U).+\.php)(.*)$;
                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }
                include fastcgi_params;
                fastcgi_param PATH_INFO       $fastcgi_path_info;
                fastcgi_param PATH_TRANSLATED $fastcgi_path_info;
                fastcgi_param  SCRIPT_FILENAME $fastcgi_script_name;
                fastcgi_pass   unix:/run/php-fpm/www.sock;
                fastcgi_index index.php;
        }

end http://host/test.php/ works, but http://host/test.php without trailing / do not work.

I try to change to such config:

   location ~ \.php$ {
           fastcgi_split_path_info ^/(.+\.php)(.*)$;
           if (!-f $document_root$fastcgi_script_name) {
                   return 404;
           }
           include fastcgi_params;
           fastcgi_param PATH_INFO       $fastcgi_path_info;
           fastcgi_param PATH_TRANSLATED $fastcgi_path_info;
           fastcgi_param  SCRIPT_FILENAME $fastcgi_script_name;
           fastcgi_pass   unix:/run/php-fpm/www.sock;
           fastcgi_index index.php;
   }

and I got 404, and get from php-fpm

Primary script unknown

So have you idea how to make http://host/test.php works without trailing /? I tried many variants, but failed to understand what is wrong. I used nginx 1.8.1

Update

1) This problem only appears if I set chroot in /etc/php-fpm.d/www.conf

2) I find out using strace that for url with test.php/ php-fpm got SCRIPT_NAME/test.php\v\n and with url test.php I got SCRIPT_NAME/test.php\v\t. So first one end with '\n' and second one end with '\t' can not get why.

Update 2 Looks like a bug, I try 1.9.13 and the same config, not working with 1.8.1, works fine.

fghj
  • 196
  • 2
  • 8

2 Answers2

1

The first block works for me (with or without trailing slash), so there may be another conflicting location in your configuration which prevents one variant from working.

The second block is not capturing the leading '/' of the URI so the test if (!-f $document_root$fastcgi_script_name) always fails.

I presume that your php-fpm configuration must already know your document root, because it is usual to define SCRIPT_FILENAME as the full path:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Richard Smith
  • 12,834
  • 2
  • 21
  • 29
0

When using chroot for PHP-FPM, you have to define SCRIPT_FILENAME relative to the chroot environment. SCRIPT_FILENAME tells PHP-FPM where to find your script.

For example, if your web site index page is in /var/www/index.php, and your chroot is set to /var/www, the SCRIPT_FILENAME has to be /index.php instead of /var/www/index.php, which is the default nginx sets.

So, you have to insert the directory inside the chroot before $fastcgi_script_name in your configuration, so that PHP-FPM will find your script.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • As you see I already done this, see line `SCRIPT_FILENAME $fastcgi_script_name` in my config, my `chroot == $document_root`, and php-fpm find file if I add `/` at the end, see again my question, so I configure in almost right way, the problem in `\n` vs `\t`, but I doubt that this is related to prefix of filename – fghj Apr 06 '16 at 17:13