编辑Nginx重写规则与CodeIgniter工作,得到500个错误


Edit Nginx rewrite rule to work with CodeIgniter and got 500 error

我尝试了stackoverflow中给出的相同问题的每个答案。它给了我一个进步,但仍然找不到最终的解决方案。

当我发布到其他控制器时,我得到了500 Internal Server Error Nginx错误。

当我检查/var/log/nginx/error.log时,它显示以下错误;

`[error] 17184#0: *8 rewrite or internal redirection cycle while internally redirecting to "/index.php/", client: 58.9.106.64, server: 60daysonline.co, request: "POST /60d/index.php/order/new_order HTTP/1.1", host: "60daysonline.co", referrer: "http://60daysonline.co/60d/"`
<<h2>上下文/h2>

OS: Ubuntu 14.04

WebServer: Nginx 1.4.6

框架:CodeIgniter 2.2.6

基础URL: http://60daysonline.co/

基本CodeIgniter目录:http://60daysonline.co/60d/

配置

文件:/etc/nginx/sites-available/违约

server {
    server_name www.60daysonline.co;
    return 301 $scheme:/60daysonline.co$request_uri;
}
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /home/him_aeng/goo_html/public_html;
    index index.php index.html index.htm;
    server_name 60daysonline.co;
    location / {
            try_files $uri $uri/ /index.php/$1?$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /home/him_aeng/goo_html_public_html;
    }
    location ~ '.(hh|php)$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+'.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params
            set $no_cache "";
            if ($request_method !~ ^(GET|HEAD)$) {
                set $no_cache "1";
            }
            if ($no_cache = "1") {
                add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
                add_header X-Microcachable "0";
            }
            if ($http_cookie ~* "_mcnc") {
                        set $no_cache "1";
            }
            fastcgi_no_cache $no_cache;
            if ($no_cache = "1") {
                add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
                add_header X-Microcachable "0";
            }
            if ($http_cookie ~* "_mcnc") {
                        set $no_cache "1";
            }
            fastcgi_no_cache $no_cache;
            fastcgi_cache_bypass $no_cache;
            fastcgi_cache microcache;
            fastcgi_cache_key $scheme$host$request_uri$request_method;
            fastcgi_cache_valid 200 301 302 10m;
            fastcgi_cache_use_stale updating error timeout invalid_header http_500;
            fastcgi_pass_header Set-Cookie;
            fastcgi_pass_header Cookie;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 16k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
            fastcgi_read_timeout 240;
    }
    location ~*  '.(ico|css|js|gif|jpe?g|png)('?[0-9]+)$ {
            log_not_found off;
            access_log off;
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
    # deny access to apache .htaccess files
    location ~ /'.ht {
            deny all;
    }
}

CodeIgniter的config。

$config['base_url']     = "";
$config['index_page'] = 'index.php';
$config['uri_protocol'] = "REQUEST_URI";

到目前为止,我所做的进展遵循这里的指令

我修改了/etc/nginx/sites-available/default,直到它看起来像这样;

location / {
            try_files $uri $uri/ /index.php;
}
location ~ [^/]'.(hh|php)(/|$) {
            fastcgi_split_path_info ^(.+'.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) { return 404; }
            ...
}

和以下是新的codeIgniter的配置文件;

$config['base_url']     = "http://60daysonline.co/60d";
$config['index_page'] = '';
$config['uri_protocol'] = "REQUEST_URI";

新问题

我发现,当我把空字符串$config['index_page'],系统会把我路由到url排除index.php地址,如 60daysonline/60day//order/new_order。它显示了404错误页面。

如果我把'index.php'到$config['index_page'], url将变成60daysonline/60day/index.php/order/new_order。我看到了"根控制器页面",这是与基础url (60daysonline/60d)相同的页面。

这似乎是系统不调用函数在控制器(在这种情况下;函数new_order在Order控制器中)。这是一个新问题,我必须不断寻找解决方案

我不知道Codeigniter,但是您的nginx配置文件有多个问题。

在第3行,您缺少一个/,它应该是:

return 301 $scheme://60daysonline.co$request_uri;

location /块的try_files语句中,您的默认操作是/index.php/$1?$args,这会出现两个问题:

  1. $1的值未定义-您是指$uri吗?
  2. 你没有位置块来处理包含路径信息的PHP uri

您的location ~ '.(hh|php)$块只看到以.php结尾的uri,因此以.php/结尾的uri将导致重定向循环。

如果你需要处理包含路径信息的PHP uri,你需要改变正则表达式并删除块中的try_files指令。详细信息请参见本文档。例如:

location ~ [^/]'.php(/|$) {
    fastcgi_split_path_info ^(.+?'.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) { return 404; }
    ...
}

或者,通过更改默认操作并删除/$1来完全避免路径信息。一些例子表明/index.php是你所需要的:

location / {
    try_files $uri $uri/ /index.php;
}