ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL链接,ThinkPHP配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的。下面详细讲解下如何让Nginx支持ThinkPHP的具体配置。
nginx.conf的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| server { listen 80; server_name www.leixuesong.com; index index.php index.html; root /path/www/web;
location /{ if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } }
location ~ \.php($|/) { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf;
#设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量 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_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } } |
修改完Nginx的配置文件一定要重启,这样,Nginx就支持ThinkPHP的PATHINFO和URL rewrite模式。