Nginx: Running WordPress in a subdirectory, when a CakePHP application is running on the root

I’m trying to use WordPress to use in a corporate blog on a separated directory, while operating a CakePHP application on the domain root. However, my attempt fails as a 404 error when trying to access the blog directory via browsers. The 404 error is originated from Nginx, not CakePHP. Below is my circumstances:

  • OS: Ubuntu 16.04 LTS
  • PHP Version: PHP 7.0.15
  • Nginx Version: nginx 1.10.0
  • What I want:
    • http://example.com to execute CakePHP, installed in: /home/example/cakephp
    • http://example.com/blog to execute WordPress, installed in: /home/example/blog
  • What I get:
    • Accessing http://example.com : 200 (CakePHP app works fine, as expected)
    • Accessing http://example.com/blog : 404 (nginx error, not CakePHP error)
  • My current nginx settings:

    server {
      listen 80;
      server_name example.com
      root   /home/example/cakephp/webroot/;
      index  index.php;
      error_log    /var/log/nginx/example.com.error.log debug;
    
      location / {
        try_files $uri $uri/ /index.php?$args;
      }
    
      location ~ .php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
    
      location @wp {
        root /home/example/blog;
        rewrite ^/blog(.*) /blog/index.php?q=$1;
      }
    
      location ~ /blog {
        root /home/example/blog;
        index index.php index.html index.htm;
        try_files $uri $uri/ @wp;
        location ~ .php$ {
          root /home/example/blog;
          try_files $uri $uri/ @wp;
          include /etc/nginx/fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          fastcgi_pass    unix:run/php/php7.0-fpm.sock;
        }
      }
    }
    

Do I have any problem in the above settings?

Read more here: Nginx: Running WordPress in a subdirectory, when a CakePHP application is running on the root

Leave a Reply

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