2017-06-18 57 views
0

我有一台运行NGINX 1.10.0的服务器,并在子目录/ ci /中安装了CodeIgniter 3。当我进入子目录时,欢迎页面呈现,但任何其他控制器发出404错误。使用CodeIgniter 3和NGINX控制器文件导致404错误

这里是在/ etc/nginx的/网站可用/默认的配置文件中的代码:

server { 
    server_name mydomain.org; 
    root /home/username/mydomain/; 
    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 /home/username/mydomain/ci/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:/run/php/php7.0-fpm.sock;; 
     } 
} 

我创建了一个Hello World控制器文件(Hello.php)

<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
class Hello extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
    } 

    public function index(){ 
     $this->load->view('helloworld'); 
    } 
} 

在我config.php文件,我有以下几点:

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

我在这里试过的方法:

NGINX笨方药:https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/

笨NGINX重写规则: http://www.farinspace.com/codeigniter-nginx-rewrite-rules/

笨nginx的404错误: Codeigniter nginx 404 error

大多数是基于PHP5或旧的,我读过的一切CodeIgniter的版本。

+0

任何CI链接与'fastcgi_param SCRIPT_FILENAME尝试/ home/username/mydomain/ci $ fastcgi_script_name;'** a nd /或**'root/home/username/mydomain/ci;'。 – Tpojka

+0

Juts在codeigniter的最新版本中提示不要让你的基础URL空白,你必须将它设置为它在基本url上面的注释中说的内容,比如'$ config ['base_url'] ='http:// localhost/yourproject /';'或'$ config ['base_url'] ='http://www.example.com/';' – user4419336

+0

谢谢@Tpojka和@ wolfgang1983!我需要调整这些设置以及Nginx配置。 –

回答

1

使用这个配置在Nginx的(Source)

server { 
      server_name domain.tld; 

      root /your/web/root/path 
      index index.html index.php; 

      # set expiration of assets to MAX for caching 
      location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { 
        expires max; 
        log_not_found off; 
      } 

      location/{ 
        # Check if a file or directory index file exists, else route it to index.php. 
        try_files $uri $uri/ /index.php; 
      } 

      location ~* \.php$ { 
        fastcgi_pass 127.0.0.1:9000; 
        include fastcgi.conf; 
      } 
    } 

而且你需要在你的PHP-FPM配置的变化。编辑这个文件:

/etc/php/7.0/fpm/pool.d/www.conf 

找到这行:

listen = /run/php/php7.0-fpm.sock 

改成这样:

listen = 127.0.0.1:9000 

这种变化应该可以解决您的问题。

+0

谢谢!这种配置帮助我解决了这个问题。由于我仍然收到一些错误,所以我不得不调整一下。 –

+0

@ xtine.k当然。快乐的编码! –

0

这是默认的www.conf和config文件的最终工作版本。我发现的另一个问题是控制器和文件名必须是相同的大小写。控制器类Hello需要使文件名为Hello。我的一些控制器的大小写不一样,但这不是404的唯一原因,因为我在修复默认文件之前尝试了这种方法。

的/ etc/nginx的/网站可用/默认:

server { 
    server_name example.org; 
    root /home/username/example/; 
    index index.php index.html index.htm; 

    location/{ 
     # Check if a file or directory index file exists, else route it to index.php 
     try_files $uri $uri/ /index.php; 

     # Handle PHP files 
     location ~* \.php$ { 
      root /home/username/example/ci; 
      include fastcgi_params; 
      fastcgi_pass 127.0.0.1:9000; 
      include fastcgi.conf; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      try_files $uri $uri/ ci/index.php; 
     } 
    } 

    # set expiration of assets to MAX for caching 
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { 
     expires max; 
     log_not_found off; 
    } 
} 

/etc/php/7.0/fpm/pool.d/www。CONF:

listen = 127.0.0.1:9000 

最后,在我的配置文件:

$config['base_url'] = 'http://example.org/ci/'; 

这种结构改变来自

http://example.org/ci/Hello 

http://example.org/Hello