來源:http://www.thinkphp.cn/topic/26657.html
第一步:配置SERVER塊
server {
listen 80;
server_name www.domain.com domain.com;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# \.php 只處理動態請求,對於靜態資源請求由下面的 location匹配和處理
location ~ \.php {
root /data0/htdocs/www;
fastcgi_pass 127.0.0.1:9000;
# 包含nginx服務器傳遞給fastcgi程序的參數,
# php中通過$_SERVER['參數名']可獲取
include fastcgi.conf;
# 定義變量$fastcgi_script_name_new賦值為$fastcgi_script_name變量
set $path_info "";
set $fastcgi_script_name_new $fastcgi_script_name;
# 判斷url是否是pathinfo形式的,如果是則把這個url分割成兩部分,
# index.php入口文件之后的pathinfo部分存入$path_info變量中,
# 剩下的部分和$document_root根目錄定位index.php入口文件在文件系統中的絕對路徑 .
if ($fastcgi_script_name ~* "^(.+\.php)(/.+)$" ) {
set $fastcgi_script_name_new $1;
set $path_info $2;
}
# 對fastcgi.conf中的SCRIPT_FILENAME和SCRIPT_NAME fastcgi參數進行重寫,
# 目的是指定入口文件在文件系統中的絕對路徑給script_filename參數,讓fastcgi知道index.php文件位置。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name_new;
fastcgi_param SCRIPT_NAME $fastcgi_script_name_new;
#定義一個新的nginx服務器傳遞給fastcgi的參數PATH_INFO,thinkphp需要這個入口文件index.php后的pathinfo信息
fastcgi_param PATH_INFO $path_info;
}
# 用來匹配靜態資源,如果不是靜態資源就重寫,然后重新輪訓所有的location塊,
# 由上面的location塊匹配后動態處理這個請求
location / {
root /data0/htdocs/www;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php$1 last;
}
}
}
第二步:打開thinkphp框架的配置文件convention.php,
修改URL_MODEL=>1,采用pathinfo模式,別設置成2啊,因為nginx重寫加上了index.php入口文件了,也就是最終發送到thinkphp的url是pathinfo模式的。
第三步:在瀏覽器輸入:www.domain.com或者www.domain.com/index.php結果如下:
:)
歡迎使用 ThinkPHP!
[ 您現在訪問的是Home模塊的Index控制器 ]
第四步:在瀏覽器中輸入URL時候,用pathinfo形式的url和用rewrite形式的url兩者中的任何都可以,或者不用這兩個框架形式的url, 用一般形式的url。例如:
pathinfo形式:
http://www.domain.com/index.php/module/controler/action/參數1/值1/參數2/值2/
rewrite形式(就是不要輸入入口文件了,其它的和pathinfo模式一樣)
http://www.domain.com/module/controler/action/參數1/值1/參數2/值2/
一般形式的url,不通過index.php入口文件啟動框架,也就是不用框架
http://www.domain.com/test.php?par1=value1&par2=value2
這時候需要在/data0/htdocs/www這個目錄下存在自定義的test.php文件,就可以直接訪問這個php文件了。
