This weekend, I wanted to change my web server from Apache to NGINX. The installation was easy, like the basic configuration, but I had a “little big” problem with CakePHP… Nothing was being displayed on my screen, or a 500 error was being shown. What should I do now?

Both CakePHP and WordPress were designed to work with Apache, because they come with some .htaccess for us. My challenge was exactly that! Rewriting the CakePHP .htaccess to do NGINX start working like Apache was working.

Opening the .htaccess from CakePHP’s root dir, we have:

Opening the .htaccess at /app:

And finally, the .htaccess from /app/webroot:

Observing these rules, the logic that I understood was: ROOT DIRECTS TO APP, THAT DIRECTS TO WEBROOT, THAT TREATS THE URL. Then I wrote a rule so that NGINX could do the same thing. Remembering that my CakePHP project was inside a subdir at the root of my web server, so if the root was public_html, my project was a subdir inside public_html. Suppose it’s name is foobar, the rule stayed like this:

If anyone knows any better way, share it on comments… I recommend the reading of this link: http://wiki.nginx.org/Pitfalls

16 thoughts on “NGINX rewrite for CakePHP

  1. Poderia disponibilizar o /etc/nginx/sites-available/default?
    Pois, comigo os arquivos do webroot/ não abrem. O nginx retorna 404.

  2. Disculpen necesito ayuda soy estudiante de I.S.C de 5 semestre necesito ayuda en la reglas que usa nginx no me las se y he buscando en google pero no he tenido éxito alguno me gustaria que me podrian enviar o darme no se una url donde pueda tener acceso a esas reglas, cuales son y para que sirve.

  3. Good Job Heitor!

    I’m still trying to figure out a way to fix CakePHP2 baseurl for static files, as css and images. My page returns /foobar/css/cake.generic.css instead of /foobar/app/webroot/css/cake.generic.css. I’m starting on version 2.2.4.

    Thanks, Marcelo

    1. Hi Heitor,

      I found my error. I had a specific location for static files, which I setup when I installed nginx for the first time according to http://www.howtoforge.com/installing-php-5.3-nginx-and-php-fpm-on-ubuntu-debian:

      Just commented that section and everything works ok now.

      Have a NIce Day!

      1. Marcelo,

        to treat static files, we can use this:

        I guess it’s a better approach to performance!

        What you think?

    1. Então Pedro, a configuração que eu uso é essa do Post mesmo… Como que é a URL do seu projeto, e como que o Cake gera a url dos css, js e imagens? Como que você está chamando essas coisas no código? Cria também um gist da sua configuração de NGINX para eu ver…

  4. Great tutorial, you’ve saved my day. I spent hours trying to configure cakephp application in my server. Thanks for the nice article.

  5. Olá Heitor.

    Não consigo fazer Rewrite funcionar no Nginx com Cakephp 3.
    Meu projeto está em uma subpasta na raiz e a parte do rewrite está assim:

    Poderia me ajudar?

    1. Olá Diego! Tudo bem?

      Desculpe a demora para responder… instalei um CakePHP 3.2 só para poder te falar o que fazer, e minha configuração final ficou assim:

      Só com isto, já funcionou!

      1. Com esta configuração os so funciona a index, o controller pages por exemplo nao, por padrao ao acessado a url retorna pra index, mas não é isso que acontece

        1. Leandro,
          testei com a mesma configuração que faço pro Laravel, e funcionou legal desta vez:

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.