标签归档:pathinfo

pathinfo配置全局生效的原因

pathinfo配置全局生效的原因后,发现部分目录转发其他服务器代理不能生效,访问的路径还是走pathinfo路径。分析原因,原来是在是否进入要加入一个location / 匹配,否则全部都会到pathinfo。比如我的www.dnsdizhi.com/mptest/public/xxx 变成请求www.dnsdizhi.com/mptest/public/index.php/xxxx

location ~ /mptest/public/ {

        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        #proxy_cache tmpcache;
        #proxy_cache_valid  200 304 1m;
        proxy_cache_key $host$uri$is_args$args;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://jingchang-api;
        proxy_pass_header Set-Cookie; 
        expires 10m;
}

                location ~ [^/]\.php(/|$)
                        {
                                # comment try_files $uri =404; to enable pathinfo
                                try_files $uri =404;
                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                fastcgi_index index.php;
                                include fastcgi.conf;
                                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 $document_root$real_script_name;
                                fastcgi_param SCRIPT_NAME $real_script_name;
                                fastcgi_param PATH_INFO $path_info;
                        }

                location ~ .*\.(php|php5)?$
                        {
                                try_files $uri =404;
                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                fastcgi_index index.php;
                                include fastcgi.conf;
                                fastcgi_read_timeout 300; 
                        }

                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                        {
                                expires      30d;
                        }

                location ~ .*\.(js|css)?$
                        {
                                expires      12h;
                        }

location / {   
                if (!-e $request_filename)
                        {
                        rewrite ^(.*)$ /index.php/$1 last;
                        break;
                        }
}

Nginx下实现pathinfo及ThinkPHP的URL Rewrite模式支持

打开Nginx的配置文件 /usr/local/nginx/conf/nginx.conf 一般是在这个路径,根据你的安装路径可能有所变化。如果你配置了vhost,而且只需要你这一个vhost支持pathinfo的话,可以直接打开你的vhost的配置文件。找到类似如下代码(不同版本的nginx可能稍有不同,但是相差不会很远): 


C代码  收藏代码

  1. location ~ .*.(php|php5)?$  
  2. {  
  3.         #原有代码  
  4. }  




C代码  收藏代码

  1. #去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo  
  2. #如果你不需要用到php5后缀,也可以将其去掉  
  3. location ~ .php  
  4. {  
  5.     #原有代码  
  6.   
  7.     #定义变量 $path_info ,用于存放pathinfo信息  
  8.     set $path_info “”;  
  9.     #定义变量 $real_script_name,用于存放真实地址  
  10.     set $real_script_name $fastcgi_script_name;  
  11.     #如果地址与引号内的正则表达式匹配  
  12.     if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {  
  13.         #将文件地址赋值给变量 $real_script_name  
  14.         set $real_script_name $1;  
  15.         #将文件地址后的参数赋值给变量 $path_info  
  16.         set $path_info $2;  
  17.     }  
  18.     #配置fastcgi的一些参数  
  19.     fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;  
  20.     fastcgi_param SCRIPT_NAME $real_script_name;  
  21.     fastcgi_param PATH_INFO $path_info;  
  22. }  





这样,nginx服务器就可以支持pathinfo了。但是如果要支持ThinkPHP的URL_MODE设置为2的模式,还需要配置rewrite规则。找到access_log语句,在其上方加上以下语句: 


C代码  收藏代码

  1. #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则  
  2. if (!-e $request_filename)  
  3. {  
  4.     #地址作为将参数rewrite到index.php上。  
  5.     rewrite ^/(.*)$ /index.php/$1;  
  6.     #若是子目录则使用下面这句,将subdir改成目录名称即可。  
  7.     #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;  
  8. }  





最后,保存配置文件,重启nginx服务,把ThinkPHP的URL_MODEL设置为2,访问下你的页面,如果能正常访问,恭喜你pathinfo配置成功了。 



贴上配置文件: 

C代码  收藏代码

  1. server {  
  2.         listen       80;  
  3.         server_name  localhost;  
  4.     index index.html index.htm index.php;  
  5.     root /home/www;  
  6.           
  7.     # think项目 增加过滤功能,支持Rewrite  
  8.     location /think {  
  9.         # ThinkPHP Rewrite, 除以上指定的静态资源外,其它的请求才有必要进行判断  
  10.         if (!-e $request_filename){  
  11.             rewrite ^/think/(.*)$ /think/index.php/$1 last;  
  12.         }  
  13.     }  
  14.   
  15.         #去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo  
  16.         #如果你不需要用到php5后缀,也可以将其去掉  
  17.     location ~ .*\.(php|php5)  
  18.     {  
  19.         #fastcgi_pass  unix:/tmp/php-cgi.sock;  
  20.         fastcgi_pass  127.0.0.1:9000;  
  21.         fastcgi_index index.php;  
  22.         include fastcgi.conf;  
  23.   
  24.         #定义变量 $path_info ,用于存放pathinfo信息  
  25.                 set $path_info “”;  
  26.                 #定义变量 $real_script_name,用于存放真实地址  
  27.                 set $real_script_name $fastcgi_script_name;  
  28.                 #如果地址与引号内的正则表达式匹配  
  29.                 if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {  
  30.                         #将文件地址赋值给变量 $real_script_name  
  31.                         set $real_script_name $1;  
  32.                         #将文件地址后的参数赋值给变量 $path_info  
  33.                         set $path_info $2;  
  34.                 }  
  35.                 #配置fastcgi的一些参数  
  36.                 fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;  
  37.                 fastcgi_param SCRIPT_NAME $real_script_name;  
  38.                 fastcgi_param PATH_INFO $path_info;  
  39.   
  40.     }  
  41.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  
  42.     {  
  43.         expires 30d;  
  44.     }  
  45.     location ~ .*\.(js|css)?$  
  46.     {  
  47.         expires 1h;  
  48.     }  
  49. }  
  50.   
  51. # 日志打印出来,查看请求数据  
  52. log_format  _access  ‘$remote_addr – $remote_user [$time_local] “$request” ‘  
  53.               ‘$status $body_bytes_sent “$http_referer” ‘  
  54.               ‘”$http_user_agent” “$http_x_forwarded_for” “$request_filename”‘;  
  55. access_log  /home/log/nginx/access/php.log _access;