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$ { try_files $uri =404; include /etc/nginx/fastcgi_params;

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 or or But then it get's weird. if I try to access, the php file is not executed, and is uploaded as a text file (downloaded by the browser)

If I try to access, 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.


Just push all not static to index.php:

server {

    listen   80;

    index index.php index.html index.htm;
    set $root_path '/var/www/';
    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;


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:

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


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?