3和NGINX的控制器文件出现404错误

3和NGINX的控制器文件出现404错误

本文介绍了使用CodeIgniter 3和NGINX的控制器文件出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行NGINX 1.10.0的服务器,并在/ci/子目录中安装了CodeIgniter 3.当我进入子目录时,欢迎页面将呈现,但其他任何控制器均会出现404错误.

I have a server running NGINX 1.10.0 and installed CodeIgniter 3 in a subdirectory /ci/. The welcome page renders when I go to the subdirectory but any other controller gives a 404 error.

这是/etc/nginx/sites-available/default配置文件中的代码:

Here is the code in /etc/nginx/sites-available/default configuration file:

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)

I created a Hello World Controller file (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文件中,我具有以下内容:

In my config.php file, I have the following:

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

我在这里尝试过这些方法:

I have tried the approaches here:

NGINX CodeIgniter食谱: https://www.nginx. com/resources/wiki/start/topics/recipes/codeigniter/

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 Rewrite Rules:http://www.farinspace.com/codeigniter-nginx-rewrite-rules/

Codeigniter Nginx 404错误: Codeigniter Nginx 404错误

Codeigniter nginx 404 error:Codeigniter nginx 404 error

我阅读的大部分内容都是基于PHP5或旧版本的CodeIgniter.

Most of everything that I've read is based on PHP5 or an older version of CodeIgniter.

推荐答案

在Nginx中使用此配置(来源):

Use this config in 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配置中进行更改.编辑此文件:

Also you need to make a change in your php-fpm config. Edit this file:

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

查找此行:

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

更改为此:

listen = 127.0.0.1:9000

此更改应该可以解决您的问题.

This changes should solve your issue.

这篇关于使用CodeIgniter 3和NGINX的控制器文件出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:39