Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite all requests for non existing files to index.php with try_files with Nginx

I am trying to convert trivial htaccess file to Nginx and can't make it work. It returns 404 error. Here is htaccess content:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Here is my current nginx config:

server {
listen 80;
server_name domain.biz;
root /var/www/domain.biz;
charset utf-8;
autoindex off;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include        fastcgi_params;
fastcgi_pass   unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
like image 744
sergeda Avatar asked Dec 16 '13 01:12

sergeda


1 Answers

  1. How about other php files you call directly? For example an info.php with just a

    phpinfo();

    inside?

    I ask this because your server conf seems to be using try_files just right, but I'm not sure you're serving php scripts right.

  2. ¿Is your fastcgi pool listening on that sock? ¿Are you sure it isn't listening in port 9000 for example? In any case, I prefer to define an upstream in the http section and use it later in the server section

    http {
    
        upstream phpbackend {
            server unix:/var/run/php5-fpm.sock;
        }
        ...
    
    
        server {
            ...
            location ~ \.php$ {
                include       fastcgi_params;
                fastcgi_pass  phpbackend;
                fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
            }    
        }
    }
    
  3. Are you sure your php.ini has the cgi.fix_pathinfo set to false?

like image 78
ffflabs Avatar answered Nov 07 '22 11:11

ffflabs