Nginx中PATH_INFO的空值返回垃圾值 [英] Empty value to PATH_INFO in nginx returns junk value
问题描述
当nginx设置的PATH_INFO为空字符串时,从PHP访问它时出现一些垃圾字符.
When the PATH_INFO set by nginx is empty string, I get some junk character while accessing it from PHP.
这是我在nginx中设置PATH_INFO的方法:
This is how I set the PATH_INFO in nginx:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
如果路径信息具有一些非空值,则可以正常工作.我什至重新安装了nginx,但没有帮助. nginx版本是1.0.5(如果需要的话,我正在使用Ubuntu 11.10).
It works fine if the path info has some non-empty value. I even reinstalled nginx, but it didn't help. nginx version is 1.0.5 (I'm using Ubuntu 11.10, if at all it matters).
推荐答案
几天前的某个视图中我得到了同样的东西..因此,我将正则表达式更改为如下形式:
I got the same thing a view days ago .. therefore I changed the regexp to look like this:
fastcgi_split_path_info ^(.+\.php)(/.*)$;
并在其他行中添加了一个视图,以使其最有可能在Apache上运行.
And added a view other lines to make it work most likely to Apache.
这是我对文件 fastcgi_params
@@ -3,13 +3,22 @@
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
-fastcgi_param SCRIPT_FILENAME $request_filename;
+#fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
+fastcgi_split_path_info ^(.+\.php)(/.*)$;
+fastcgi_param PATH_INFO $fastcgi_path_info;
+set $path_translated "";
+if ($fastcgi_path_info) {
+ set $path_translated $document_root$fastcgi_path_info;
+}
+fastcgi_param PATH_TRANSLATED $path_translated;
+fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
使用此配置,您将始终拥有PATH_INFO变量,而不是在f.e中如何完成.阿帕奇.
Using this configuration you always have the PATH_INFO variable instead of how it's done in f.e. Apache.
我使用过的某些脚本像这样检查(当然)不适用于我的配置:
Some of the scripts I used just checked like this what (of course) does not work with my configuration:
if (!isset($_SERVER['PATH_INFO']) { doSomething() }
我建议主要开发人员将其更改为此:
I adviced the main-developer to change it to this:
if (!isset($_SERVER['PATH_INFO'] || empty($_SERVER['PATH_INFO']) { doSomething() }
如果要查看我的完整服务器配置,只需查看以下github存储库: https://github.com/SimonSimCity/webserver-configuration/
If you want to take a look im my full-server-configuration, just take a look in this github-repository: https://github.com/SimonSimCity/webserver-configuration/
编辑:我找到了一个解决方案稍有不同的博客.我还没有测试过,但是看起来有点小;) http://www.jzxue.com/fuwuqi/http -iis-apache/201108/19-8538.html
I found a blog with a slightly different solution. I have not tested it yet, but it seems to be a bit smaller ;) http://www.jzxue.com/fuwuqi/http-iis-apache/201108/19-8538.html
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME /var/html/$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
这篇关于Nginx中PATH_INFO的空值返回垃圾值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!