ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的,所以我们需要修改nginx.conf文件。

网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 'URL_MODEL' => 2 的情况了

 

  1. server    
  2.      {    
  3.              listen       80;    
  4.              server_name  www.xxx.com;    
  5.              index index.html index.htm index.php index.shtml;    
  6.              root /data2/www/www.xxx.com;    
  7.   
  8.              location / {    
  9.                   if (!-e $request_filename){    
  10.                        rewrite ^(.*)$ /index.php?s=/$1 last; #rewrite模式    
  11.                        rewrite ^(.*)$ /index.php/$1 last; #pathinfo模式 任选其一    
  12.                    }    
  13.              }    
  14.   
  15.   
  16.              location ~ \.php {    
  17.   
  18.                   fastcgi_pass    127.0.0.1:9000;    
  19.                   fastcgi_split_path_info ^(.+\.php)(.*)$;    
  20.                   fastcgi_param PATH_INFO $fastcgi_path_info;    
  21.                   fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;    
  22.                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
  23.                   include         fastcgi_params;    
  24.                   fastcgi_connect_timeout 300;    
  25.                   fastcgi_send_timeout 300;    
  26.                   fastcgi_read_timeout 300;    
  27.              }    
  28.   
  29.   
  30.      }