CakePHP and WordPress on Nginx PHP-FPM

iDev247 asked:

I’m trying to configure both CakePHP and a wordpress blog on the same server.

CakePHP here: http://site.com/

WordPress blog here: http://site.com/blog/

What works: The whole CakePHP app and going to /blog/.

What doesn’t work: Going to /blog/permalink/. It gives a CakePHP 404 page.

The /blog/ works with or without the “# Blog config” below. How do I make /blog/permalink/ work? I’m used to working with Apache.

edit: It was suggested that my question is a duplicate of this post however if I use that solution or my solution (commented # Blog config) below it gives me a CakePHP 404 page. This means that /blog/permalink/ doesn’t hit the wordpress’s index.php.

upstream backend {
    server unix:/var/www/apps/appname/tmp/php.sock;
}

server {
    listen 80 default;
    root    /var/www/apps/appname/public/app/webroot;
    index   index.php index.html index.htm;

    server_tokens off;

    access_log  /var/www/apps/appname/logs/access.log;
    error_log   /var/www/apps/appname/logs/error.log;

    client_max_body_size 20M;

    rewrite_log on;

    # Blog config
    location /blog/ {
        try_files $uri $uri/ /blog/index.php?$args;
    }

    # Not found this on disk? 
    # Feed to CakePHP for further processing!
    if (!-e $request_filename) {
        rewrite ^/(.+)$ /index.php last;
        break;
    }

    # Pass the PHP scripts to FastCGI server
    # listening on 127.0.0.1:9000
    location ~ .php$ {
        fastcgi_pass   backend;
        fastcgi_index  index.php;
        fastcgi_intercept_errors on; # to support 404s for PHP files not found
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # Static files.
    # Set expire headers, Turn off access log
    location ~* favicon.ico$ {
        access_log off;
        expires 1d;
        add_header Cache-Control public;
    }
    location ~ ^/(img|cjs|ccss)/ {
        access_log off;
        expires 7d;
        add_header Cache-Control public;
    }

    location ~ ^/(php_status|php_ping)$ {
      fastcgi_pass backend;
      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
      include fastcgi_params;
      allow 127.0.0.1;
      deny all;
    }

    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }

    # Deny access to .htaccess files,
    # git & svn repositories, etc
    location ~ /(.ht|.git|.svn) {
        deny  all;
    }
}

My answer:


This looks like your problem. It rewrites all URLs, not just those destined for CakePHP. This is one of the most common nginx misconfigurations.

    # Not found this on disk? 
    # Feed to CakePHP for further processing!
    if (!-e $request_filename) {
        rewrite ^/(.+)$ /index.php last;
        break;
    }

This should be deleted and replaced with an equivalent try_files in your location / block (which you don’t seem to have, so create one).

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

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.