【问题标题】:Edit Nginx rewrite rule to work with CodeIgniter and got 500 error编辑 Nginx 重写规则以使用 CodeIgniter 并得到 500 错误
【发布时间】:2016-12-19 00:23:29
【问题描述】:

我尝试了在 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/"`

上下文

操作系统:Ubuntu 14.04

网络服务器:Nginx 1.4.6

框架:CodeIgniter 2.2.6

基本网址:http://60daysonline.co/

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

配置

文件:/etc/nginx/sites-available/default

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.php

$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'] 时,系统会将我路由到将index.php 从地址中排除的url,例如 60daysonline/60day//order/new_order。它显示了 404 错误页面。

如果我将 'index.php' 放入 $config['index_page'],则 url 将变为 60daysonline/60day/index.php/order/new_order。我看到了“根控制器页面”,它与基本 url (60daysonline/60d) 是同一页面。

似乎系统根本没有调用控制器中的函数(在这种情况下;订单控制器中的函数 new_order)。这是我必须不断寻找解决方案的新问题。

【问题讨论】:

  • 我想尝试一次,将base_url 删除并删除index.php
  • @VivekSingh 当我根据您的建议更改 config.php 时,系统会将我发送到404 page not found 页面。

标签: php .htaccess codeigniter nginx


【解决方案1】:

我不知道 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 指令。有关详细信息,请参阅this document。例如:

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;
} 

【讨论】:

  • 哇,非常感谢您的清晰回答。我确实按照您的建议进行了修改。我取得了一些进展,发现新的问题出现了。是MVC问题不兼容Nginx的配置文件。
猜你喜欢
  • 2015-03-04
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2017-07-26
  • 2020-01-10
相关资源
最近更新 更多