讓Nginx支援ThinkPHP的URL重寫和PATHINFO
實現讓ThinkPHP在nginx上正確運行。
只需在設定檔中添加以下資訊,就能讓nginx正確解析ThinkPHP的網站。
?
?
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; include fastcgi.conf; fastcgi_index index.php?IF_REWRITE=1; 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錯誤。
?
作者:?潺莪?發表於 2011-08-08 10:48?原文連結
?