We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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_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 https://my.domain.dev or https://my.domain.dev/ or https://test.nis.dev/public/index.php But then it get's weird. if I try to access https://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 https://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.



2.8k
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;
    }
}


24.8k

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; }



24.8k

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,

i have a same problem.

can you show nginx conf ?

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; }