nginx的高性能优势明显,自己又很喜欢pathinfo类型的url。
apache下mod_rewirte有很好的实现,nginx就捉鸡了。
网上有一些中文的文章教你怎么配置nginx来支持pathinfo(当然很多是互相抄袭的)
英文的相对少很多
这里贴下我的配置:
首先是location / 的块里面,对于不存在的请求文件名,全部转发给index.php,这样便不在404
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php$1 last; break; } }
此外,还需要几行字来把pathinfo提取出来,写在location ~ \.php$ 块里面,注意为了支持pathinfo
这个块匹配的正则需要改为 location ~ \.php($|/) ,这是正则相关知识自行查阅
location ~ \.php($|/) { set $script $uri; set $path_info ""; if ($uri ~ "^(.+\.php)(/.+)") { set $script $1; set $path_info $2; } root R:\\; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_NAME $script; include fastcgi_params; }
看到root设置你知道这个是windows上面的配置,没错就是我本机的设置,R盘不用惊讶,那是内存盘,只是配置了ide里面的自动上传,方便调试。下面给出这个配置过后的相关的$_SERVER信息。
输出显示pathinfo信息被附加到好多个变量里面,有点乱,其实主要参考是request_uri和path_info,去别在于前者带有queryString参数,后者没有
["DOCUMENT_URI"]=> string(16) "/index.php/1/2/3" ["REQUEST_URI"]=> string(6) "/1/2/3" ["SCRIPT_NAME"]=> string(16) "/index.php/1/2/3" ["PATH_INFO"]=> string(6) "/1/2/3" ["SCRIPT_FILENAME"]=> string(13) "R:\/index.php"