Nginx rewrite rules for lithium php framework

nginxphp-fpmrewrite

I have been trying to get lithium running with Nginx with no success. The directory structure of lithium is like this

lithium
  |-> app libraries .htaccess
      |-> webroot .htacess --other directories
           |-> index.php .htaccess --other files

I copied the lithium folder into my /var/www/. The path of lithium is /var/www/lithium/

Now I set up the rewrite rules this way in my nginx.conf

server {
        listen       80;
        server_name  localhost;
        root /var/www/;

    location /lithium {
       rewrite ^/$ /app/webroot/ break;
       rewrite ^(.*)$ /app/webroot/$1 break;
    }

    location /lithium/app {
       rewrite ^/$ /webroot/ break;
       rewrite ^(.*)$ /webroot/$1 break;
     }

    location /lithium/app/webroot {
       if (!-e $request_filename){
        rewrite ^(.*)$ /index.php?url=$1 break;
        }
     }
 ...then my php and other configurations

But nginx always throws a 404 error. Why is that happening?

I also tried this

server {
            listen       80;
            server_name  localhost;
            root /var/www/;

location /lithium {
        if (!-e $request_filename) {
            rewrite ^/lithium/(.+)$ /lithium/app/webroot/$1 last;
            break;
            }
        }

        location /lithium/app/webroot {
        if (!-e $request_filename) {
            rewrite ^/lithium/app/webroot/(.+)$ /lithium/app/webroot/index.php?url=$1 last;
            break;
        }
    }
...then my php and other configurations

But again there is a 404 error.

Edit

As suggested i changed my server's root to /var/www/lithium/app/webroot so my nginx conf looks like this

server {
        listen   80;
        server_name localhost;

        root   /var/www/lithium/app/webroot;
        access_log /var/log/lithium/access.log;
        error_log /var/log/lithium/error.log warn;

        index  index.php index.html;

        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$
        {
          .. ...
           ......

}

now i can see lithium's home but when i go to lithium's test dashboard which is http://localhost/test it again shows lithium's home instead of the test dashboard.When i use apache and go to url http://localhost/test it shows me the test dashboard. So the nginx rewrite rules are still not completely correct.More if i point root to lithium's webroot i cannot access other directories in my /var/www/

EDITED AGIAN

This is my complete server block

server { 
    server_name  lithium;
    root   /var/www/lithium/app/webroot;
    access_log  /var/log/nginx/lith.access.log;
    error_log   /var/log/nginx/lith.error.log;

    listen       127.0.0.2:80;
    rewrite_log on;

    # rewrite rules for lithium
    location / {
            index  index.php index.html;

            try_files $uri $uri/ /index.php?url=$uri&$args;
    }


    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #include /etc/nginx/fastcgi_params;
            fastcgi_param SERVER_NAME $host;
    }

    location ~ /\.ht {
            deny  all;
    }
}

I am using php 5.4.3. as php-fpm. I tried doing whats mentioned at lithium's official site here but i dont get this line

cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

which location is sapi referring to? Any ideas?

Best Answer

It'd be simpler to just point to webroot using root /var/www/lithium/app/webroot.

Or you can do the following.

root /var/www

then use try_files instead of ugly if's:

index  index.php index.html;

location ~ \.php$ {
    ...
}

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

location /lithium/app/ {
    rewrite ^/lithium/app/(.*)$ /lithium/app/webroot/$1;
}

location /lithium/ {
    rewrite ^/lithium/(.*)$ /lithium/app/webroot/$1;
}

location / {
    rewrite ^(.*)$ /lithium/app/webroot/$1;
}

Don't forget to protect the other directories.