Reputation: 2211
I'm using Nginx and Codeigniter alongside php5-fpm. Everything "seems" to work just fine, pages are shown and everything looks great. Ofcource this isn't the reason I'm asking this question here, I actually have a problem.
The problem I'm facing is that 404 error is thrown with page, even though the page is rendered properly, I'm stilling getting 404's(in the logs).
The reason why I'm getting 404's (after looking at Nginx's error logs), is that Nginx can't open the file I'm requesting, because Nginx tries to open the PHP files directly, instead of referring to the controller/method
and unfortunately Codeigniter works that way.
For example:
requesting http://website.com/<controller>/<function>/<other_params>
results in 404 in Nginx logs, the reason behind this is that open()
can't open the specified directory because it doesn't exists, instead of referring to the controller/method
.
Some important logs:
Nginx error log:
[error] 4172#0: *482 open() "/var/www/<domain>/public/site/section/main" failed
(2: No such file or directory), client: <client_ip>, server: <domain>, request: "GET
/site/section/main HTTP/1.1", host: "<domain>"
as I said before, Nginx is trying to access the file directly, instead of making Codeigniter deal with it.
my sites-enabled/ci
configurations:
server
{
server_name <domain> *.<domain>;
access_log /var/www/<domain>/access.log;
error_log /var/www/<domain>/error.log;
root /var/www/<domain>/public;
index index.php index.html index.htm;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/site(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
# if (!-d $request_filename)
# {
# rewrite ^/(.+)/$ /$1 permanent;
# }
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/(system|application))
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
#if (!-e $request_filename)
#{
# rewrite ^/(.*)$ /index.php?/$1 last;
# break;
#}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php($|/)
{
#if (!-e $request_filename) {
# return 404;
# }
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# location / {
# try_files $uri $uri/ @codeigniter;
#}
# location @codeigniter {
# rewrite ^(.*) /index.php?$1 last;
# }
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
So, what's the reason behind this misunderstanding between Nginx and Codeigniter?
Thanks in advance.
Upvotes: 0
Views: 2483
Reputation: 20753
It seems like there's no rule in your config that tells nginx to try sending requests to php when the file is not found. There's a commented out block with try_files
and @codeigniter
that almost look like it. In theory this is what you want nginx do:
For this, these two blocks should be enough:
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
# for security, see http://forum.nginx.org/read.php?2,88845,page=3
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The other if
guarded block shouldn't break with these in place.
Upvotes: 2