让 nginx 提供 PATH_INFO,并实现 PATH_INFO 重写

我采用nginx + php-fpm的配置,nginx默认情况下,不提供PATH_INFO,通过以下设置,可以让 nginx 提供 PATH_INFO,实现  PATH_INFO 重写:

 

 1 server {
 2     listen 80; 
 3     server_name www.sample.com;
 4     root /www;
 5     index index.php index.html;
 6     location / { 
 7         index index.php index.html index.htm;
 8 
 9         #以下部分实现重写
10         if (!-e $request_filename) {
11             rewrite ^/(.*)$ /index.php/$1 last;
12         }
13         #也可以写在一个独立的文件中,然后include进来,如下一行
14         #include /www_rewrite/.htaccess;
15 
16     }   
17     location ~ \.php($|/) {
18         fastcgi_pass 127.0.0.1:9000;
19         fastcgi_index index.php;
20         fastcgi_buffers 8 128k;
21         send_timeout 60; 
22         include /etc/nginx/fastcgi_params;
23 
24         #这一部分实现PATH_INFO,使用$_SERVER['PATH_INFO']获取该值
25         set $script_name $fastcgi_script_name;
26         set $path_info ""; 
27         if ($uri ~ "^(.+?\.php)(/.*)$") {
28                set $script_name $1; 
29                set $path_info $2; 
30         }   
31         fastcgi_param PATH_INFO $path_info;
32         fastcgi_param SCRIPT_NAME $script_name;
33         #至此,已完成PATH_INFO的配置
34     }   
35 }

 

posted on 2012-06-05 11:52  还是刀哥靠谱  阅读(625)  评论(0编辑  收藏  举报

导航