我有一臺運行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的版本。
任何CI鏈接與'fastcgi_param SCRIPT_FILENAME嘗試/ home/username/mydomain/ci $ fastcgi_script_name;'** a nd /或**'root/home/username/mydomain/ci;'。 – Tpojka
Juts在codeigniter的最新版本中提示不要讓你的基礎URL空白,你必須將它設置爲它在基本url上面的註釋中說的內容,比如'$ config ['base_url'] ='http:// localhost/yourproject /';'或'$ config ['base_url'] ='http://www.example.com/';' – user4419336
謝謝@Tpojka和@ wolfgang1983!我需要調整這些設置以及Nginx配置。 –