Codeigniter Nginx控制器函数返回404

时间:2016-02-14 18:02:55

标签: php .htaccess codeigniter nginx

我目前正在服务器上运行nginx和codeigniter。 我可以访问基本功能,例如

sample.dev/samplecontroller

但是当我尝试在该控制器中指定一个函数时,例如

sample.dev/controller/function

我收到404错误

我的站点 - 项目sample.dev的可用文件如下:

server {
listen 80 ;
listen [::]:80;

root /var/www/html/sample_ci;

# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;

server_name sample.dev;

      location / {
           # URLs to attempt, including pretty ones.
           try_files   $uri $uri/ /index.php?$query_string;
    }
   error_page 404 /404.html;
   error_page 500 502 503 504 /50x.html;
   location = /50x.html {
     root /usr/share/nginx/html;
    }

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#   include snippets/fastcgi-php.conf;
    try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
            fastcgi_split_path_info         ^(.+\.php)(.*)$;

            include                        /etc/nginx/fastcgi_params;
            fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}
}

项目的.htaccess文件如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sample_ci

RewriteCond %{REQUEST_URI} ^system.*

RewriteRule ^(.*)$ /HCMP/index.php/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule> 

我已经配置了config.php文件变量,如下所示:

$config['base_url'] = 'http://sample.dev/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

尽管我遵循官方nginx网站上的所有步骤以及我在此处找到的其他帖子,但上述配置仍无法正常运行。 有什么事我做错了或应该做什么?

1 个答案:

答案 0 :(得分:0)

您不能在nginx上使用.htaccess文件......这些文件主要用于Apache。因此,您的重写未被使用,您将始终获得404。

您需要知道的是:

https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/#
http://www.farinspace.com/codeigniter-nginx-rewrite-rules/