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;