thinkphp部署在nginx下 500錯誤或404錯誤解決辦法

來源:互聯網
上載者:User


nginx是一個高效能並發的伺服器軟體,配置方面要稍微比apache複雜一點點。
本地部署成功的一個thinkphp架構,部署到伺服器的時候,剛開始因為許可權問題拋出404錯誤,然後chown之後,拋出了500的錯誤,但是首頁能夠訪問。
複查,應該是偽靜態rewrite出現問題了,找了好多文獻,解決方案如下:

應該將

location ~ .*\.(php|php5)?$
{
#fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}


更改為:

location ~ \.php
{
fastcgi_pass   127.0.0.1:9000;
fastcgi_index index.php;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME /mnt/khdb1/wwwroot$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include  /alidata/server/nginx/conf/fastcgi_params;
}

解決方案的思路:是由於在配置網站的時候,conf檔案應該將整個對伺服器的請求更改路徑,即從“ location ~ \.php” 這個開始的大括弧內的,預設的nginxlocation行頭location ~ .*\.(php|php5)?$也要改。包括第一行的location都要修改為最上面。就可以解決拋出的500伺服器錯誤“thinkphp 500錯誤”

nginx下運行出錯404錯誤-找不到檔案解決辦法


解決方案一:修改ThinkPHP設定,不使用PATH_INFO

解決方案二(推薦):修改nginx設定,支援PATH_INFO

修改nginx.conf和fastcgi-params

php geshi">

location ~ \.php$ {

修改為

location ~ \.php/?.*$ {

fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_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_root2$fastcgi_script_name2;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

修改為

fastcgi_param SCRIPT_NAME $fastcgi_script_name2;

完整的nginx虛擬機器主機區塊配置如下:

server {
listen 80;
server_name www.amiku.cn amiku.cn;
root /www_amiku_cn;
include /www_amiku_cn/.htaccess;
index index.html index.htm index.php;
location ~ .*\.(php|php5)/?.*$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
set $path_info “”;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ “^(.+?\.php)(/.+)$”) {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME /www_amiku_cn$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fcgi.conf;
}
}

另外附貼修改過的.htaccess,以滿足nginx的偽靜態和apache伺服器的規則不同。


#RewriteEngine on
#RewriteRule ^index.html$ index.php/Index/index
#RewriteRule ^([A-Z][a-z]+).html$ index.php/$1/index
#RewriteRule ^([A-Z][a-z]+)-([a-z]+).html$ index.php/$1/$2
#RewriteRule ^([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([0-9]+).html$ index.php/$1/$2/$3/$4
#RewriteRule ^([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([\x00-\xff]+)-([a-z]+)-([0-9]+)-([a-z]+)-([0-9]+).html$ index.php/$1/$2/$3/$4/$5/$6/$7/$8 rewrite ^(.*)/index.html$ $1/index.php/Index/index;
rewrite ^(.*)/([A-Z][a-z]+).html$ $1/index.php/$2/index;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+).html$ $1/index.php/$2/$3;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([0-9]+).html$ $1/index.php/$2/$3/$4/$5;
rewrite ^(.*)/([A-Z][a-z]+)-([a-z]+)-([a-z]+)-([\x00-\xff]+)-([a-z]+)-([0-9]+)-([a-z]+)-([0
-9]+).html$ $1/index.php/$2/$3/$4/$5/$6/$7/$8/$9;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.