【问题标题】:Codeigniter nginx 404 errorCodeigniter nginx 404错误
【发布时间】:2014-03-25 04:52:48
【问题描述】:

我不确定这个问题以前被回答过多少次,但我看到的每个答案都给出了解决这个问题的不同方法,但这些方法都没有奏效。我正在从 Apache 迁移到 Nginx,并且在设置它时遇到了一些严重的问题。我的 /etc/nginx/sites-available/default 看起来像这样...

server {
    #listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www/flo2go/;
    index index.php index.html index.htm;
        if ($request_filename !~ (js|css|images|robots\.txt|index\.php.*) ) {
            rewrite ^/(.*)$ /index.php/$1 last;
        }

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to index.html

                try_files $uri $uri/ /index.php;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
location ~ /index.php/
  {
    include /etc/nginx/fastcgi_params;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/www/flo2go/index.php;
    fastcgi_param  REQUEST_URI      $request_uri;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_pass   unix:/var/run/php5-fpm.sock;
  }
    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #    # With php5-cgi alone:
    #    fastcgi_pass 127.0.0.1:9000;
    #    # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

我已尽一切努力使我的 Web 应用程序正常工作。我得到的只是 404:Page Not Found 错误。该站点在 Apache 上运行良好,在花了将近 3-4 个小时解决这个问题后,我认为最好在这个论坛上寻求专家的建议。希望有人能帮我摆脱这种情况:(

【问题讨论】:

  • 为什么 php 有 2 个位置?删除location ~ /index.php/,如果你想location /doc没用,重新加载nginx再试一次
  • 这个答案解决了我的问题。 stackoverflow.com/a/46188637/5554633

标签: codeigniter configuration nginx


【解决方案1】:

适合您情况的正确配置必须与此类似:

server {
    server_name yoursitename.com;
    root /usr/share/nginx/www/flo2go/;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        expires           15d;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/www/flo2go/index.php;
        fastcgi_param  REQUEST_URI      $request_uri;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
     }
}

当前配置的主要问题是 2 个 .php location 块和 ifevil

【讨论】:

  • 谢谢,以上配置节省了我的时间。我使用 php 7 fpm、nginx 和 codeigniter。
  • @Dmitry 我已添加到 nginx 配置中,但现在正在下载该页面
【解决方案2】:

请将以下几行添加到您的配置中

if (!-e $request_filename) {
      rewrite ^.*$ /index.php last;
}

对我有用

【讨论】:

  • 这对我也有用。我知道这不是 最好的 方法,但在我弄清楚如何不使用 if 语句之前它会起作用
  • 我这样做了,现在我得到了“拒绝访问”。我会尝试解决方法。
  • 通过这个脚本,项目运行良好,但我无法运行任何其他 php 文件。就像我已经创建并想要执行但它没有执行的 phpinfo.php 文件。
  • 它在 ubuntu 中与 nignx 1.8 一起使用。谢谢
【解决方案3】:

检查控制器、视图、模型和数据库的命名。

如果您在控制器中执行此操作:

$this->load->view('main');

那么你的视图文件名必须和你在控制器中写的一样:

main.php

【讨论】:

    【解决方案4】:

    nginx 比 apache 对文件名大小写更挑剔。

    我的控制器被称为 Proto,并在一个名为 proto.php 的文件中。我将它重命名为Proto.php,它开始工作了。

    Nginx 对控制器文件名的大小写敏感。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2013-05-23
      • 1970-01-01
      • 2013-01-29
      • 2016-06-23
      • 2017-09-24
      • 2023-04-03
      相关资源
      最近更新 更多