【问题标题】:Nginx CodeIgniter not workingNginx CodeIgniter 不工作
【发布时间】:2013-05-09 13:32:48
【问题描述】:

我是 nginx 新手。我正在将我的服务器从 apache 转移到 nginx,但我在 CodeIgniter 核心 PHP 站点上的许多项目运行良好,但 CodeIgniter 无法运行。

我的示例网址是这样的:

http://example.com/track/

这是重定向到:

http://example.com/track/index.php/sessions/login

但它返回 404 Not Found。

我的服务器配置如下:

server {
    listen       80;
    server_name  192.168.0.80;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }



    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
       deny  all;
    }
}

我的错误日志文件是这样的

2013/05/15 10:21:37 [error] 2474#0: *3 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.11, server: 192.168.0.80, request: "GET /favicon.ico HTTP/1.1", host: "192.168.0.80"
2013/05/15 10:21:37 [error] 2474#0: *1 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nginx/html/index.php (No such file or directory)" while reading response header from upstream, client: 192.168.0.11, server: 192.168.0.80, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.0.80"
2013/05/15 10:22:05 [error] 2474#0: *1 open() "/usr/share/nginx/html/track/index.php/sessions/login" failed (20: Not a directory), client: 192.168.0.11, server: 192.168.0.80, request: "GET /track/index.php/sessions/login HTTP/1.1", host: "192.168.0.80"
2013/05/15 10:26:46 [error] 2474#0: *5 open() "/usr/share/nginx/html/track/index.php/sessions/login" failed (20: Not a directory), client: 192.168.0.11, server: 192.168.0.80, request: "GET /track/index.php/sessions/login HTTP/1.1", host: "192.168.0.80"
2013/05/15 10:28:33 [error] 2474#0: *7 open() "/usr/share/nginx/html/track/index.php/sessions/login" failed (20: Not a directory), client: 192.168.0.11, server: 192.168.0.80, request: "GET /track/index.php/sessions/login HTTP/1.1", host: "192.168.0.80"
2013/05/15 10:29:59 [error] 2497#0: *1 open() "/usr/share/nginx/html/track/index.php/sessions/login" failed (20: Not a directory), client: 192.168.0.11, server: 192.168.0.80, request: "GET /track/index.php/sessions/login HTTP/1.1", host: "192.168.0.80"

怎么了?我在 google 中进行了搜索,但效果不佳。

【问题讨论】:

  • 好的,几个快速的问题,这是本地服务器吗?这个 codeIgniter 项目是否在您的服务器根目录的子文件夹中运行?如果是这样,它会改变一些事情
  • 这是本地服务器。我正在使用 centos for & codigniter 在子文件夹上运行

标签: mysql codeigniter configuration nginx php


【解决方案1】:

我今天早些时候遇到了类似的问题:代码点火器 URI 路由要求同时定义 REQUEST_URI 和 SCRIPT_NAME 变量。许多 Nginx/PHP-FPM 指南没有定义 SCRIPT_NAME 变量,因此最终会导致路由失败。您需要添加如下所示的一行:

fastcgi_param  SCRIPT_NAME $fastcgi_script_name;

然后您可以在 config.php 文件中使用 REQUEST_URI 并且路由应该可以工作。

【讨论】:

    【解决方案2】:

    查看此链接,应该可以解决您的重写问题。
    如果您还有其他问题可以提问。
    Code igniter for nginx

    编辑:
    好的,有几种方法可以解决您的情况,但我将描述最接近您情况的方法

    如果您要在与 nginx conf 相同的文件中进行编辑,就像我认为您现在正在做的那样,请尝试添加此文件

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

    不确定路由是否需要将$request_uri 附加到index.php。 而且我认为您应该配置 CodeIgniter 以了解此子文件夹

    $config['base_url'] = "192.168.0.80/track"
    

    这并不是最干净的配置方式,我更喜欢在/etc/hosts 中添加一个域名,并在 nginx 中创建一个新的单独服务器。

    【讨论】:

    • 将这两行替换为fastcgi_pass unix:/var/run/php5-fpm.sock;include fastcgi_params;
    • 当我调用这个 URL 192.168.0.80/track 重定向到 192.168.0.80 并且我的核心 PHP 项目不工作时,事情变得最糟糕
    • 是的,显然您在端口而不是套接字上使用fastcgi,因此您可以保留fastcgi_pass 127.0.0.1:9000; 的行并删除fastcgi.conf 的行并将其替换为include fastcgi_params;,我我正在重写我的答案以获得更详细的版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2014-04-15
    • 2012-12-02
    • 1970-01-01
    相关资源
    最近更新 更多