配置Nginx支援ThinkPHP的URL重寫和PATHINFO
ThinkPHP支援通過PATHINFO和URL rewrite的方式來提供友好的URL,只需要在設定檔中設定 'URL_MODEL' => 2 即可。在Apache下只需要開啟mod_rewrite模組就可以正常訪問了,但是Nginx中預設是不支援PATHINFO的,所以我們需要修改nginx.conf檔案。
網上搜了很多方法都不奏效,研究了一天,發現通過以下的配置可以完美支援 'URL_MODEL' => 2 的情況了01 location /project/ {
?index index.php;?
?if (!-e $request_filename) { ?
?rewrite ^/project/(.*)$ /project/index.php/$1 last;?
?break;?
?}?
?}?
??
?location ~ .+\.php($|/) {?
?set $script $uri;?
?set $path_info "/";?
?if ($uri ~ "^(.+\.php)(/.+)") {?
?set $script $1;?
?set $path_info $2;?
?}?
??
?fastcgi_pass 127.0.0.1:9000;?
?fastcgi_index index.php?IF_REWRITE=1;?
?include /APMServ/nginx/conf/fastcgi_params;?
?fastcgi_param PATH_INFO $path_info;?
?fastcgi_param SCRIPT_FILENAME $document_root/$script;?
?fastcgi_param SCRIPT_NAME $script;?
?}
這裡先把project下的請求都轉寄到index.php來處理,亦即ThinkPHP的單一入口檔案;然後把對php檔案的請求交給fastcgi來處理,並且添加對PATH_INFO的支援。
重啟Nginx以後,http://localhost/project/Index/insert, http://localhost/project/index.php/Index/delete 這樣的URL都可以正確訪問了。
還有一個地方需要注意的是,Nginx設定檔裡 if 和後面的括弧之間要有一個空格,不然會報unknown directive錯誤。這個害我調了很久
?
1 樓 wpeng1123 2011-04-10
我的專案檔是test ThinkPHP放在test下面 我這樣改動之後 訪問登入頁面,它跳轉到index.php/Public/login 但是卻發生系統錯誤
系統發生錯誤
您可以選擇 [ 重試 ] [ 返回 ] 或者 [ 回到首頁 ]
[ 錯誤資訊 ]
無法載入模組Public
求解
2 樓 Jocson 2011-11-22
一樣啊。。。。
3 樓 mengdejun 2011-11-22
路徑配置不正確。