Nginx下支援ThinkPHP的Pathinfo跟URl Rewrite模式

來源:互聯網
上載者:User
Nginx下支援ThinkPHP的Pathinfo和URl Rewrite模式

我的環境

系統 : ? ?Ubuntu12.04 ? ?x86_64

環境 : ? ?Nginx1.1.19+PHP5.3.10+Mongo2.6.3

由於公司要用Nginx+Mongo+PHP,所以我要把剛剛配置好的LAMP推翻,然後重新安裝LNMP。軟體安裝就不在這裡介紹了,如果有需要,可以看這裡。

如何安裝Nginx

下面介紹如何使Nginx支援ThinkPHP的Pathinfo和URL Rewrite模式。

1、ThinkPHP給出了ThinkPHP的官方解決方案,如下:

開啟Nginx的設定檔 /etc/nginx/nginx.cof 一般是在這個路徑,根據你的安裝路徑可能有所變化。如果你配置了vhost,而且只需要你這一個vhost支援pathinfo的話,可以直接開啟你的vhost的設定檔。找到類似如下代碼(不同版本的nginx可能稍有不同,但是相差不會很遠):

    location ~ .php        {                #原有代碼                                #定義變數 $path_info ,用於存放pathinfo資訊                set $path_info "";                #定義變數 $real_script_name,用於存放真真實位址                set $real_script_name $fastcgi_script_name;                #如果地址與引號內的Regex匹配                if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {                        #將檔案地址賦值給變數 $real_script_name                        set $real_script_name $1;                        #將檔案地址後的參數賦值給變數 $path_info                        set $path_info $2;                }                #配置fastcgi的一些參數                fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;                fastcgi_param SCRIPT_NAME $real_script_name;                fastcgi_param PATH_INFO $path_info;        }

這樣,nginx伺服器就可以支援pathinfo了。但是如果要支援ThinkPHP的URL_MODE設定為2的模式,還需要配置rewrite規則。找到access_log語句,在其上方加上以下語句:

    #如果請求既不是一個檔案,也不是一個目錄,則執行一下重寫規則    if (!-e $request_filename)    {            #地址作為將參數rewrite到index.php上。            rewrite ^/(.*)$ /index.php/$1;            #若是子目錄則使用下面這句,將subdir改成目錄名稱即可。            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;    }

以上方法雖然是ThinkPHP官方給出的,想必也是經過驗證的,但悲催的是對我並不起作用。

2、我的解決方案

我是在sites(vhost)下配置的,在/etc/nginx/sites-available/目錄下。當然你也可以直接在/etc/nginx/nginx.conf裡配置。
然後在php配置欄目中添加如下兩行:

fastcgi_split_path_info ^(.+\.php)(.*)$;                             fastcgi_param PATH_INFO $fastcgi_path_info; 

完整配置如下

 location ~ \.php$ {                root /var/www;                try_files $uri = 404;                fastcgi_split_path_info ^(.+\.php)(/.+)$;        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini        #        #       # With php5-cgi alone:                #fastcgi_pass 127.0.0.1:9000;                fastcgi_split_path_info ^(.+\.php)(.*)$;                fastcgi_param PATH_INFO $fastcgi_path_info;        #       # With php5-fpm:                fastcgi_pass unix:/var/run/php5-fpm.sock;                fastcgi_index index.php;                include fastcgi_params;        }

大寶日記著作權,轉載請註明出處。
原文地址:http://www.sundabao.com/nginx%E4%B8%8B%E6%94%AF%E6%8C%81thinkphp%E7%9A%84pathinfo%E5%92%8Curl-rewrite%E6%A8%A1%E5%BC%8F/

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.