nginx:500 Internal Server Error

    |     2015年10月31日   |   学习偶记   |     1 条评论   |    7718

首先说明一下,这是一个nginx rewrite配置不当引起的500错误。因为学习CodeIgniter(CI),要去掉那个index.php显示,配置了conf,引起这个错误。

配置片段:

[sourcecode language=”plain”]

location / {
error_page 404 /404.html;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}

}

[/sourcecode]

经过摸索及网上经验,改为如下配置,则显示正常:

[sourcecode language=”plain”]
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /CI的目录/index.php?_url=/$1;
}
[/sourcecode]

转一篇其他人写的文章备用 原文地址:http://www.chenyudong.com/archives/codeigniter-in-nginx-and-url-rewrite.html
作者:

东东东 陈煜东

codeigniter(CI)是一个轻量型的PHP优秀框架,但是它是在apache服务器下开发的,在nginx下需要特别的配置才可以使用。

codeigniter修改

application/config/config.php进行修改,大约在48行左右。

1
$config['uri_protocol'] = "PATH_INFO";

修改nginx配置

对nginx的进行配置,nginx.conf

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
        listen       80;
        listen [::]:80 ipv6only=on;
        server_name  www.example.com;
        root   /data/www/www.example.com;
        index index.php  index.html index.htm;
        location / {
                # 这里使用try_files进行url重写,不用rewrite了。
                try_files $uri $uri/ /index.php?$query_string;
        }
        location ~ \.php($|/) {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param   PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /\.ht {
                deny  all;
        }
}

要特别注意19行的include fastcgi_params;,如果没有这一行,那么你的PHP程序会无法运行的。我被这个坑了很多次了。

仅有 1 条评论

  1. 2016-5-7 21:40
    if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; }