2017-06-18 80 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