我有一台运行NGINX 1.10.0的服务器,并在子目录/ ci /中安装了CodeIgniter 3。当我转到子目录时,欢迎页面呈现,但任何其他控制器都会出现404错误。
以下是/ etc / nginx / sites-available / default配置文件中的代码:
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 Controller文件(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 CodeIgniter Recipe:https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
CodeIgniter NGINX重写规则: http://www.farinspace.com/codeigniter-nginx-rewrite-rules/
Codeigniter nginx 404错误: Codeigniter nginx 404 error
我读过的大部分内容都是基于PHP5或旧版CodeIgniter。
答案 0 :(得分: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
此更改应解决您的问题。
答案 1 :(得分:0)
以下是默认的www.conf和配置文件的最终工作版本。我发现的另一个问题是控制器和文件名必须是相同的大小写。控制器类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/';
此配置会更改
中的所有CI链接http://example.org/ci/Hello
到
http://example.org/Hello