網上通用解決方法的配置如下:
server { ... location / { index index.htm index.html index.php; #訪問路徑的文件不存在則重寫URL轉交給ThinkPHP處理 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php/?.*$ { root /var/www/html/website; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #加載Nginx默認"服務器環境變量"配置 include fastcgi.conf; #設置PATH_INFO並改寫SCRIPT_FILENAME,SCRIPT_NAME服務器環境變量 set $fastcgi_script_name2 $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") { set $fastcgi_script_name2 $1; set $path_info $2; } fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2; fastcgi_param SCRIPT_NAME $fastcgi_script_name2; } }
其實應該使用更簡單的方法,fastcgi模塊自帶了一個fastcgi_split_path_info指令專門用來解決此類問題的,該指令會根據給定 的正則表達式來分隔URL,從而提取出腳本名和path info信息,使用這個指令可以避免使用if語句,配置更簡單。
另外判斷文件是否存在也有更簡單的方法,使用try_files指令即可。
server { ... location / { index index.htm index.html index.php; #如果文件不存在則嘗試TP解析 try_files $uri /index.php$uri; } location ~ .+\.php($|/) { root /var/www/html/website; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #設置PATH_INFO,注意fastcgi_split_path_info已經自動改寫了fastcgi_script_name變量, #后面不需要再改寫SCRIPT_FILENAME,SCRIPT_NAME環境變量,所以必須在加載fastcgi.conf之前設置 fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; #加載Nginx默認"服務器環境變量"配置 include fastcgi.conf; } }
轉載:http://blog.csdn.net/tinico/article/details/18033573
修改thinkphp讓他可以在nginx上運行
最近在用thinkphp做一個項目,基本完成后部署到nginx服務器上才發覺nginx是不支持pathinfo的,網上搜索了別人的解決方法,有兩種思路:
1、修改thinkphp讓他可以在nginx上運行
2、修改nginx讓它支持pathinfo
網上說nginx開啟pathinfo是有一定風險的,能不用pathinfo最好不用,所以還是折騰thinkphp吧,個人覺得這種方法相對第2種方法來得簡單
修改nginx的rewrite
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
然后項目配置下url模式改為2
'URL_MODEL'=>2,
如果是多個項目,布署項目時要把項目布署到目錄里,如后台的項目放到Admin目錄里,那么在nginx的rewrite里再寫一條
location /Admin/ { if (!-e $request_filename) { rewrite ^/Admin/(.*)$ /Admin/index.php?s=$1 last; break; } }
最后也不要忘記把這個項目的url模式改為2。
轉載:http://www.3lian.com/edu/2012/12-14/49363.html
Nginx環境
在Nginx低版本中,是不支持PATHINFO的,但是可以通過在Nginx.conf中配置轉發規則實現:
location / { // …..省略部分代碼 if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
其實內部是轉發到了ThinkPHP提供的兼容模式的URL,利用這種方式,可以解決其他不支持PATHINFO的WEB服務器環境。
如果你的ThinkPHP安裝在二級目錄,Nginx的偽靜態方法設置如下,其中youdomain是所在的目錄名稱。
location /youdomain/ { if (!-e $request_filename){ rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=$1 last; } }
轉載:http://doc.thinkphp.cn/manual/hidden_index.html