最完美解決Nginx部署ThinkPHP項目的辦法

網上通用解決方法的配置以下:php

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指令便可。html

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/18033573nginx

修改thinkphp讓他能夠在nginx上運行

最近在用thinkphp作一個項目,基本完成後部署到nginx服務器上才發覺nginx是不支持pathinfo的,網上搜索了別人的解決方法,有兩種思路:web

  一、修改thinkphp讓他能夠在nginx上運行正則表達式

  二、修改nginx讓它支持pathinfothinkphp

  網上說nginx開啓pathinfo是有必定風險的,能不用pathinfo最好不用,因此仍是折騰thinkphp吧,我的以爲這種方法相對第2種方法來得簡單服務器

  修改nginx的rewritedom

location / { 
if (!-e $request_filename) { 
rewrite ^(.*)$ /index.php?s=$1 last; 
break; 
} 
}

  而後項目配置下url模式改成2url

'URL_MODEL'=>2,

若是是多個項目,佈署項目時要把項目佈署到目錄裏,如後臺的項目放到Admin目錄裏,那麼在nginx的rewrite裏再寫一條spa

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

相關文章
相關標籤/搜索