nginx配置PATH_INFO模式
我们可以使用PATH_INFO来代替Rewrite来实现伪静态页面, 另外不少php框架也使用PATH_INFO来作为路由载体
在Apache中, 当不加配置的时候, 对于PHP脚本, Accept pathinfo是默认接受的
PATH_INFO是服务器状态中的一个参数,通过$_SERVER['PATH_INFO']可以查看内容
apache下配置如下
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).html$ index.php/$1 [QSA,PT,L]
比如你访问 http://127.0.0.1/pathinfo/index.html
但是nginx上有一些$_SERVER变量不支持 比如,$_server['http_x_forwarded_for'] 、$_SERVER['PATH_INFO']
这样一来对于严重依赖PATH_INFO的框架,就很麻烦了
比如Thinkphp
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
给出了这么一种方案,这也只是折衷而已了
nginx中一般的配置为
location ~* \.php$ {
fastcgi_pass unix:/dev/shm/php-fpm.socket;
fastcgi_index index.php;
fastcgi_param SCRipT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include fastcgi_params;
}.
php本身有一个cgi.fix_pathinfo配置选项,不过由于安全问题已经弃用了
看了看网上的资料,nginx中有一个fastcgi_param配置文件用来配置fastcgi_param
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏
fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name
#fastcgi_param PATH_INFO $path_info;#可自定义变量
# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;
fastcgi_param PHP_VALUE "auto_prepend_file=/data/header.php";
看到没有fastcgi_param上的参数都是$_SERVER变量的,另外还可以来配置php.ini
所有结合nginx自身的语法就有了
新版本的nginx也可以使用fastcgi_split_path_info指令来设置PATH_INFO,旧的方式不再推荐使用,在location段添加如下配置
location ~ ^.+.php {
(...)
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
(...)
}
对了,nginx+lua 也可以配置的