0

I have 2 cakephp applications: one is using cake 2 and the other is using cake 3.

This is my nginx config

server {
        listen 80;
        client_max_body_size 2M;
        server_name cake.dev;
        root /var/virtual/cake2app/webroot;
        location /cake3-app/ {
                alias /var/virtual/cake3app/webroot;
        }

        access_log /var/log/nginx/cakephpsite.com-access.log;
        include common.conf;
        include cakephp.conf;
}

This is common.conf

index index.html;

location ~ /\.ht {
        deny all;
}
sendfile off;

This is cakephp.conf

include php.conf;

location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        expires max;
        access_log off;
}

This is php.conf

location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
}

index index.php;

the cake.dev is correctly pointing to my cake 2 app.

I cannot get cake.dev/cake3-app to point to the cake 3 app.

Inside my cake 3 app, I have a users/login action which works perfectly if I access the cake 3 from a separate domain.

But that is not what I want.

What have I done wrong in terms of the nginx config?

My error is consistently a 403 if I access cake.dev/cake3-app/ and I get a cake error message telling me there is no such controller when I access cake.dev/cake3-app.

Please advise.

EDIT:

I manage to use this trick. Inside my cakedev.conf

I wrote

server {
        listen 80;
        client_max_body_size 2M;
        server_name cake.dev;
        root /var/virtual/cake2/webroot;
        access_log /var/log/nginx/cakephpsite.com-access.log;
        include common.conf;
        include cakephp.conf;
        location /cake3-app/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://127.0.0.1:83;
                proxy_redirect off;
                rewrite ^/cake3-app/(.*)$ /$1 break;
        }
}

Then I have a cake3.conf

server {
        listen 83;
        client_max_body_size 2M;
        server_name 127.0.0.1;
        root /var/virtual/cake3/webroot;
        include common.conf;
        include cakephp.conf;
}

The url redirect works for the web pages but NOT the various assets of the cake3 app.

Cake3App auto points to http://cake.dev/css/base.css when it should be pointing to http://cake.dev/cake3/css/base.css

Perhaps I need to write something different for the common.conf and the cakephp.conf for the cake3.conf?

Kim Stacks
  • 461
  • 1
  • 6
  • 14
  • Why use proxy pass at all? You could just as easily do `location /cake3-app { root /var/virtual/cake3/webroot; ... }` – AD7six Jan 11 '15 at 10:36
  • i tried that before and it didn't work for some reason. Let me try again. I think it didn't work because I needed to do the rewrite so I can reuse common.conf and cakephp.conf – Kim Stacks Jan 12 '15 at 05:43
  • Yup it didn't work. I got a 404 – Kim Stacks Jan 12 '15 at 05:58
  • in my last line, i did write 'Perhaps I need to write something different for the common.conf and the cakephp.conf for the cake3.conf?' So I supposed you are referring to this. I am not particularly clear about how to rewrite teh cakephp.conf for this use case. By the way, the common.conf, cakephp.conf, and php.conf setup was taught to me by the CakeDC people, (graham weldon I think) a few years ago when I hired their help. Be happy to rewrite the configurations if you could show me how. – Kim Stacks Jan 13 '15 at 11:14

1 Answers1

0

There are 3 steps. First 2 are nginx-related. Last one is cakephp-related.

Step 1: Need to inform the config responsible for server_name http://cake.dev to redirect http://cake.dev/cake3 urls to the right config

Assuming cakedev.conf is the config responsible for http://cake.dev

server {
        listen 80;
        client_max_body_size 2M;
        server_name cake.dev;
        root /var/virtual/cake2/webroot;
        access_log /var/log/nginx/cakephpsite.com-access.log;
        include common.conf;
        include cakephp.conf;
        location /cake3-app/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://127.0.0.1:83;
                proxy_redirect off;
                rewrite ^/cake3-app/(.*)$ /$1 break;
        }
}

Notice how I write the proxy_pass? It goes to 127.0.0.1:83. This is crucial though I suspect you can change the port number.

Step 2: Write up the config responsible for cake3

Assuming the file is cake3.conf

server {
        listen 83;
        client_max_body_size 2M;
        server_name 127.0.0.1;
        root /var/virtual/cake3/webroot;
        include common.conf;
        include cakephp.conf;
}

Notice how the server_name and the listen matches with the proxy_pass from the earlier config? This is crucial.

Step 3: Change the App.base inside cake3

Go inside your cake 3 app and look for config/app.php

Change this value

'App' => [
    'namespace' => 'App',
    'encoding' => 'UTF-8',
    'base' => false,

to

'App' => [
    'namespace' => 'App',
    'encoding' => 'UTF-8',
    'base' => '/cake3-app', // please make sure you start with a / and NEVER end with a /
Kim Stacks
  • 461
  • 1
  • 6
  • 14