|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-03 04:42 UTC] konstantin at symbi dot org
Description: ------------ FPM status does not work when cgi.fix_pathinfo = 1, with the default nginx configuration for php (default fastcgi_params + SCRIPT_FILENAME added). The problems is that in fpm_main.c, here: if (!strcasecmp(SG(request_info).request_method, "GET") && fpm_status_handle_status(SG(request_info).request_uri, ... SG(request_info).request_uri is sometimes NULL depending on the trickiest things which happen when cgi.fix_pathinfo = 1. I have examined the code of init_request_info() which runs when cgi.fix_pathinfo = 1, and I found out that this part of code is legacy part from the CGI SAPI, it is was designed to fix problems with a lots of broken CGI implementations. It was reasonable for CGI to get the things working independent on speed, but the 'bruteforce' approach used is not too good for FPM SAPI which is commonly used on high-bandwidth sites. More, PHP introduces non-standard "SCRIPT_FILENAME" fastcgi env; it is not hard to add it to configuration of flexible servers like nginx (by using "fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name" in its configuration), but it can cause problems with such servers as lighttpd where all the fastcgi parameters are hardcoded (I've seen people on forums complaining thay get 'no input file' with nginx+fpm and they found no way to fix it). So, I have found the fix_pathinfo part is written in a hard to understand and difficult to fix way, and that is is really not required for FPM SAPI, and that it slows it down. System administrators who configure FSM webservers and use FPM sapi are usually experianced and for them it would be preferable to have strict and simple logic rather then some magic which can potentially slow things down a lot. So i have rewritten init_request_info(), the the suggested patch is attached. It logic of detection script_filename is much simpler: if SCRIPT_FILENAME is defined, use it, else if both DOCUMENT_ROOT and SCRIPT_NAME are defined, concat them and use (should fix all the problems with lighty etc); else if PATH_TRALSLATED is defined, use it as script_filename; else we do not know the script_filename. And, of course, it fixes the problem with pm.status_path I started with. Test script: --------------- N/A Expected result: ---------------- N/A Actual result: -------------- N/A Patchesinit_request_info.patch (last revision 2010-06-03 02:43 UTC by konstantin at symbi dot org)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 18:00:01 2025 UTC |
99% of fpm installations are with nginx or lighty, using fpm sapi with non-FSM webservers is at least very strange. But I have just checked it with Apache+mod_fastcgi in a simplest configuration: DocumentRoot "/var/www" FastCgiExternalServer /var/www -socket /tmp/php-fpm.sock <Directory /var/www> Options FollowSymLinks +ExecCGI AllowOverride All Order Allow,Deny Allow from all </Directory> and it works OK. The patch contains comments in the top of the new init_request_info() implementation. It describes why fastcgi parameters are mapped to the script filename in this way, and it was chosen after examining CGI specs, typical fpm configurations, and common sense. BTW, those webservers like old IIS versions with buggy cgi implementations which required that awkward way of guessing what they meant, for which the original implementation of init_request_info() was designed, do not support remote FastCGI at all.Warning: the following comment is very long. Take time to read it and don't hesitate to ask me for details questions. Notes: I've not been able to put it all in one comment (it's detected as spam). So I've split it into several comments. I just review de patch and there is a problem. It does not work with mod_fastcgi except when mod_fastcgi is configured as commented before: DocumentRoot "/var/www" FastCgiExternalServer /var/www -socket /tmp/php-fpm.sock <Directory /var/www> Options FollowSymLinks +ExecCGI AllowOverride All Order Allow,Deny Allow from all </Directory> Setting this make all requests to be forward to php-fpm and that is definitely NOT what common configurations aim to do. More common mod_fastcgi configuration would be something like: ScriptAlias /fcgi-bin/ /usr/local/apache2/fcgi-bin/ FastCGIExternalServer /usr/local/apache2/fcgi-bin/php-cgi -host 127.0.0.1:9000 AddHandler php-fastcgi .php Action php-fastcgi /fcgi-bin/php-cgi and in this case, the patch does not work.Hello, Here are a few quick thoughts. 1) The fix_pathinfo stuff has been implemented a long ago, and it's main purpose was to workaround the bugs of web servers used 10 years ago. It was developed with the CGI exec()s in mind so the performance impact caused by multiple stat()s was not so important. I see no reason to keep it nowadays. 2) The patch I have proposed hase a bug mentioned in a comment above, that must be fixed. I personally just use fix_patninfo=0 now ;) 3) The CGI protocol itself has been developed (as far as I understand) with a thought that there's some monolithic application which takes PATH_INFO, parses it, does something and prints the results. With PHP applications, there's usually another case - we need to map the request variables to a physical path to the php script, the same way as web server SAPIs do. It does not conform to any RFCs but that's how people DO use PHP, and that's a behavior everyone expects in 99.9999% cases. 4) The non-standard SCRIPT_FILENAME fastcgi variable is widely used in many configurations, and there are standard config samples for nginx etc which rely on the fact that it has been working for years. 5) Your proposal seems mostly OK but I'd prefer if the SCRIPT_FILENAME remains supported. My proposal would be close to yours: I. Add the 'fcgi.accept_script_filename' per-pool ini setting, default true; II. Add the document_root.override per-pool ini setting, default empty. III. Remove all the fix_pathinfo stuff, and change the corresponding parts of the init_request_info function according to the pseudocode: function get_script_filename(ini, Env) { var script_filename; if (ini["fcgi.accept_script_filename"] == true && Env["SCRIPT_FILENAME"] is not empty) { script_filename = Env["SCRIPT_FILENAME"]; } else { doc_root = undefined; assert(Env["SCRIPT_NAME"] is not empty); // * if (ini["document_root.override"] is not empty) { doc_root = ini["document_root.override"]; } else { assert(Env["DOCUMENT_ROOT"] is not empty); doc_root = Env["DOCUMENT_ROOT"]; } script_filename = concat(doc_root, Env["SCRIPT_NAME"]); } return script_filename; } *) assert() means 'respond with status 500 if assertion fails'. The RFC3875 compliance can be achieved by defining document_root.override and setting fcgi.accept_script_filename = false.After having analyzing comments, apache behaviour, here is what I propose for this case. 1- add a fpm configuration directive for each pool named "fastcgi_client". Possible values are "rfc3875", "apache_mod_fastcgi", "apache_mod_proxy_fcgi" or "backward_compatibility". Defaults to "rfc3875". 2- add a fpm configuration directive for each pool named "document_root". It can be set to a directory. Defaults to (null). 3- add a fpm configuration directive for each pool named "always_run_script". It can be set to a php file. Defaults to (null) When a request is received: /* * Use a custom script to do some routage and other stuf * In this case FPM does nothing but to passthrough fastcgi variables */ if (fpm.ini[always_run_script] is set) { return execute_php(fpm.ini[always_run_script]) } /* * override DOCUMENT_ROOT if document_root is set in the FPM config * otherwise check the DOCUMENT_ROOT sent is a valid directory */ if (fpm_ini[document_root] is set) { DOCUMENT_ROOT = fpm.ini[document_root] // no need to check if DIR exits as it's been done at conf check } else { if (DOCUMENT_ROOT is not set) { return error500 "document root not set" } if (!is_dir(DOCUMENT_ROOT)) { return error500 "Documentroot not found or not a directory" } } if (fpm.ini[fastcgi_client] == "backward_compatibility") { /* * Use the same code as before (with microsoft clean up) * Will maybe be removed in a later release. */ see fpm_main.c in function init_request_info() }else if (fpm.ini[fastcgi_client] == "apache_mod_fastcgi" or "apache_mod_proxy_fcgi") { /* * *** mod_fastcgi *** * * SCRIPT_NAME is invalid (/php5-fcgi) * PATH_INFO is set to /test.php/more * SCRIPT_FILENAME is invalid (/tmp/php5-fcgi) * DOCUMENT_ROOT is set correctly * PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO * REQUEST_URI is set correctly * QUERY_STRING is set correctly * * ==> use DOCUMENT_ROOT + PATH_INFO */ /* * *** mod_proxy_fcgi *** * * everything is buggy in mod_proxy_fcgi * PATH_TRANSLATED is set only if proxy-fcgi-pathinfo is set * PATH_TRANSLATED is set to "proxy:fcgi://host:port" + PATH_INFO * sometimes PATH_TRANSLATED is set to "proxy:fcgi://host:port" + PATH_INFO*2 * --> we can rely on PATH_TRANSLATED no matter what * * PATH_INFO is set only if proxy-fcgi-pathinfo is set * PATH_INFO is set to /test.php/more * SCRIPT_NAME is empty if proxy-fcgi-pathinfo is set * SCRIPT_NAME has the same value as PATH_INFO when proxy-fcgi-pathinfo is set * --> we can rely on one of those value. They are the same depending on * proxy-fcgi-pathinfo * * SCRIPT_FILENAME value is consistent and set to * "proxy:fcgi://host:port" + PATH_INFO * * DOCUMENT_ROOT is set correctly * QUERY_STRING is set correctly * * ==> we will use PATH_INFO or SCRIPT_NAME and DOCUMENT_ROOT */ if (fpm.ini[fastcgi_client] == "apache_mod_fastcgi") { unset SCRIPT_NAME /* will use PATH_INFO instead */ } /* automatic detection for mod_proxy_fcgi */ if (SCRIPT_NAME null or empty) { SCRIPT_NAME = PATH_INFO unset PATH_INFO } if (SCRIPT_NAME is empty) { return error500("SCRIPT_NAME or PATH_INFO not set") } /* override PATH_TRANSLATED */ PATH_TRANSLATED = DOCUMENT_ROOT + SCRIPT_NAME if (php.ini[fix_pathinfo] == 1) { /* * Try to determine SCRIPT_FILENAME and PATH_INFO from PATH_TRANSLATED */ For each '/' in PATH_TRANSLATED begining from the end of the string { SCRIPT_FILENAME = path before the '/' PATH_INFO = path after the '/' (with the '/' included) if SCRIPT_FILENAME is a valid file { PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO return execute_php(SCRIPT_FILENAME) } return error404 "file not found" } } else { /* * Suppose PATH_TRANSLATED is DOCUMENT_ROOT + SCRIPT_NAME * ignore PATH_TRANSLATED and PATH_INFO */ SCRIPT_FILENAME = PATH_TRANSLATED unset PATH_TRANSLATED return execute_php(SCRIPT_FILENAME) } } else { /* rfc3875 */ /* * simple as descript in RFC 3875 * let PATH_INFO and PATH_TRANSLATED to their value * change nothing as we are supposed to be rfc compliant :-) */ if (SCRIPT_NAME not set) { return error500 "SCRIPT_NAME not set" } SCRIPT_FILENAME = DOCUMENT_ROOT + SCRIPT_NAME return execute_php(SCRIPT_FILENAME) } - "rfc3875" makes things very easy and quick for nginx, lighthttpd and other compliant fastcgi clients. php.ini fix_pathinfo is not used in this case. - "apache_mod_fastcgi" and "apache_mod_proxy_fcgi" makes the code cleaner for apache. Correct values are deducted. php.ini fix_pathinfo is still used. If path_info URL are not used, fix_pathinfo can be disabled to avoid doing useless and costy calls to stats(). It would be maybe possible to use only one value "apache" and detect which one is used. But I prefer not to to be able to differentiate them later if need (as proxy_mod_fcgi could hopefully be updated to fix some of its bugs) - "backward_compatibility" for those who have older version of apache or else which is not compatible with the previous mode. Hopefully it won't be needed and will be removed in a later release. what guys do you think ??? ++ jerome> Do the issues with apache_mod_fastcgi still exist? I remember I've tested it > with my patch and it worked well, maybe you've missed some of its settings? Using a global mod_fastcgi configuration (where everything is sent to FPM) returns full compliant fastcgi envars which are compatible with the "rfc3875" mode. Using a specific mod_fastcgi configuration (AddHandler, AddAction and Alias) returns buggy fastcgi envars which are compatible with the "apache_mod_fastcgi" mode. > Personally I don't like introducing such "magic" settings which are unclear on > what they do. It reminds me of that crappy "broken-scriptfilename" and "fix- > root-scriptname" lighttpd fastcgi settings. I indeed prefer the nginx way where > every fastcgi variable is configured explicitly. I hear you. I don't really mind since the "magic" settings are documented and explain what they do. A simple explication "use apache_mod_fastcgi is your are using mod_fastcgi using ScriptAlias, apache_mod_proxy_fcgi if your using mod_proxy_fcgi, compliant otherwise. If you encouters problems, you can back to backward_compatibility". And then a more specific explication "apache_mod_fastcgi: The SCRIPT_FILENAME is deduced using DOCUMENT_ROOT + PATH_INFO. If fix_pathinfo is set, then a reverse search is made on the string to find out the SCRIPT_FILENAME part and the PATH_INFO part by testing (using stat()3) the SCRIPT_FILENAME to ensure it exists on disk." "apache_mod_proxy_fcgi: the SCRIPT_FILENAME is deduced using DOCUMENT_ROOT + (PATH_INFO || SCRIPT_NAME). Auto detection is used wether to choose PATH_INFO or SCRIPT_NAME (it depends on the proxy-fcgi-pathinfo settings). If fix_pathinfo is set, the same treatment is done than it's done for apache_mod_fastcgi" "rfc3875: SCRIPT_FILENAME is deduced using DOCUMENT_ROOT + SCRIPT_NAME. No other actions are taken as the PATH_INFO stuff is done by the web server" "backward_compatibility: old code untouched. Will maybe be removed in later major release" > In general it's OK, but I world prefer more obvious settings with names telling > about real fastcgi parameters. It's the matter of taste of course. What do you have in mind ? Another possibility would be to set the transormation rules inside of fpm configuration with something like: envvar[SCRIPT_FILENAME] = "%{DOCUMENT_ROOT}%{SCRIPT_NAME}" envvar[SCRIPT_NAME] = "%{PATH_INFO} and keep the usage of fix_pathinfo. If set, SCRIPT_FILENAME is searched for real file and PATH_INFO. Otherwise nothing is done. In the comments, for each kind of configuration (mod_fastcgi, mod_proxy_fcgi, nginx, lighttpd) the right settings are documented. In the case, nothing is changed, try to make autodetection based on SERVER_SOFTWARE maybe. But, even if it's more detailed it's maybe too complex. > Anyway, there are two things to take into account: known clients, and backward > compatibility for any possible configuration. We need to remember that this is > _remote_ fastcgi, and php-fpm may run on a different machine. For instance, I > have seen a real life nginx configuration which looked like: > > set $remote_php_root /path/to/remote/php/root; > SCRIPT_FILENAME $remote_root_php/$fastcgi_script_name; > > and DOCUMENT_ROOT was defined to the front web server root. It is senseless, but > it worked: SCRIPT_FILENAME was correct, and the php code did not use > $_SERVER['DOCUMENT_ROOT'] at all. In all the cases we can't match all the possible solutions, especially ones for nginx as everything can be setup as desired. Such a configuration is marginal, I think, and a little change won't kill anyone (I think). > Also, these changes really should not go to php 5.4 (or go with "compat" mode by > default). There already have been a BC-breaking change with >security.limit_extensions in minor update, I believe no one wants one more. Agree. The code will be put in 5.3, 5.4 and 5.5 but the default settings will be set to "rfc3875" only for 5.5 (if it's not been released yet).