Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Nginx + php + rewrite url

Hi there,

I'm trying to settup a simple url-rewriting on a nginx/fpm-php config/phalcon. I'm planning to create a multi-module platform (public , api, admin) and I'd like my visitors requests to be redirected to /public/index.php in case no module is requested.

So I've added the following in my nginx config:

... location / { rewrite ^$ /public/ break;

rewrite ^/$ /public/ break;

rewrite !^/(public|api|admin)/$ /public/$1 break;

}

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

location ~ .php$ { tryfiles $uri =404; include /etc/nginx/fastcgiparams;

fastcgi_pass    127.0.0.1:9000;
fastcgi_index   index.php;
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param   SERVER_NAME $host;
...

} ...

It works fine if i try to access http://my.domain.dev or http://my.domain.dev/ or http://test.nis.dev/public/index.php But then it get's weird. if I try to access http://my.domain.dev/public/, the php file is not executed, and is uploaded as a text file (downloaded by the browser)

If I try to access http://my.domain.dev/index.html, I get the phalcon (?) message : "Mod-Rewrite is not enabled Please enable rewrite module on your web server to continue"

So it seems something is wrong, I could help some help.



1.5k
Accepted
answer

Just push all not static to index.php:

server {

    listen   80;
    server_name example.dev;

    index index.php index.html index.htm;
    set $root_path '/var/www/example.dev/public';
    root $root_path;

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

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/etc/php5/fpm/socket/wtm.sock; // here set yours php-fpm, mine is through unix socket
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }
}


10.4k

Thank you, worked. I thought I had to keep the root folder above /public. I've pushed a bit more adding a cascaing rule, and now I'm all good:

tryfiles $uri $uri/ @phalcon; location @phalcon { rewrite ^/(.*)$ /index.php?url=/$1; }



10.4k

A quick follow up regarding the root settings.

Now my document_root is /public, I'm getting stuck with logs. I've tried to use \Phalcon\Logger\Adapter\File('') to save error logs into a file, but it doesn't seem to work outside the /public folder.

I don't particulary want to save logs in the public folder... so what's the solution?

best,