Codeigniter nginx 404 error

23,178

Solution 1

Right config for your situation must look simular to this:

server {
    server_name yoursitename.com;
    root /usr/share/nginx/www/flo2go/;
    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  /usr/share/nginx/www/flo2go/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:/var/run/php5-fpm.sock;
     }
}

Main problem with current config is 2 .php location blocks and if which is evil.

Solution 2

please add following lines to your config

if (!-e $request_filename) {
      rewrite ^.*$ /index.php last;
}

it works for me

Solution 3

nginx is more picky about case of file names than apache.

my controller was called Proto and in a file called proto.php. I renamed it to Proto.php and it started working.

Nginx is sensitive about case of controller file name.

Solution 4

Check the naming s of you Controller , View, Model and Database.

if you do this in your controller:

$this->load->view('main');

then the filename of your view must be same as you wrote in your controller:

main.php
Share:
23,178
Admin
Author by

Admin

Updated on August 27, 2020

Comments

  • Admin
    Admin over 3 years

    I am not sure how many times this question has been answered before, but every answer that I look at gives a different approach to solving this problem of which none of them worked. I am migrating from Apache to Nginx and am facing some serious problems with setting it up. My /etc/nginx/sites-available/default looks like this...

    server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6
    
        root /usr/share/nginx/www/flo2go/;
        index index.php index.html index.htm;
            if ($request_filename !~ (js|css|images|robots\.txt|index\.php.*) ) {
                rewrite ^/(.*)$ /index.php/$1 last;
            }
    
        # Make site accessible from http://localhost/
        server_name localhost;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.html
    
                    try_files $uri $uri/ /index.php;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
    location ~ /index.php/
      {
        include /etc/nginx/fastcgi_params;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/www/flo2go/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:/var/run/php5-fpm.sock;
      }
        location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            deny all;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #    # 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;
            include fastcgi_params;
        }
    
    }
    

    I have tried everything to make my web application work. All I keep getting is the 404:Page Not Found error. The site was working perfectly on Apache and after spending almost 3-4 hours in solving this problem I thought that it would be better to seek the advise of experts on this forum. Hope somebody can bail me out of this situation :(

  • AJMaxwell
    AJMaxwell about 8 years
    This worked for me as well. I know it's not the best way to do it, but it will work until I can figure out how not to use an if statement
  • Deepan Prabhu Babu
    Deepan Prabhu Babu almost 7 years
    Thank you, above configuration saved my day. I use php 7 fpm, nginx and codeigniter.
  • Marcelo Agimóvel
    Marcelo Agimóvel over 5 years
    I did that and now I got "access denied". I'll try a workaround.
  • manish1706
    manish1706 over 4 years
    by this script, project runs well, but i am not able to run any other php file. like phpinfo.php file i have created and wanted to execute but it does not execute.
  • C.V
    C.V over 2 years
    It's worked with nignx 1.8 in ubuntu. thank you
  • Mr Coder
    Mr Coder over 2 years
    @Dmitry i added into nginx config but now downloading that page
  • Dmitry Verhoturov
    Dmitry Verhoturov about 2 years
    @MrCoder I think, your setup might be missing include mime.types;