Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter nginx 404 error

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 :(

like image 909
Seemant Mathur Avatar asked Nov 28 '22 21:11

Seemant Mathur


2 Answers

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.

like image 51
Dmitry Verhoturov Avatar answered Dec 05 '22 08:12

Dmitry Verhoturov


please add following lines to your config

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

it works for me

like image 36
Beta Avatar answered Dec 05 '22 07:12

Beta