2

I'm trying to add on cakephp on to an existing server, but the location / block is being used. I'm following the pretty url on nginx section on the cakephp cookbook. On my test environment, I have the server block looking like

server {
    listen 80;
    server_name localhost;


    access_log /var/www/html/log/access.log;
    error_log /var/www/html/log/error.log;


    location / {
        root /var/www/html/cakephp/app/webroot/;
        index index.php;

        try_files $uri $uri/ /index.php?$args;

    }


    location ~ \.php$ {
        root /var/www/html/cakephp/app/webroot/;
        index index.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;
    }
}

From this, I'm able to run my testsController through the url localhost/tests

However, the server that I'm trying to add cakephp to, there is already another application installed at the domain root.

location / {
    proxy_pass https://localhost/somepage;
}   

I tried setting up a location block like

location /cakephp {
    root /var/www/html/cakephp/app/webroot/;
    index index.php;
    try_files $uri $uri/ /index.php?args;
}

I understand that this wouldn't work because it's looking for cakephp in the url, which it wouldn't be there. Since root is set to be /var/www/html/cakephp/app/webroot, when I access the url localhost/cakephp, is it looking for /var/www/html/cakephp/app/webroot/cakephp?

I'm getting confused about how to set this up. I read about url rewriting and cakephp running in some subdirectory, but I'm not sure if that's what I am looking for. Right now, the application runs with http://localhost/someController. I would like to have the the application run with the url http://localhost/cakephp/someController. How should I setup my nginx config?

AD7six
  • 63,116
  • 12
  • 91
  • 123
bobby8767
  • 113
  • 1
  • 5
  • @AD7six Accessing the url localhost/cakephp, it says the CakephpController is not found. I created the controller and is able to hit the correct webpage. Meaning I have to create a new location block for each controller? Hmm...Is there a way to set the url to use localhost/cakephp instead of localhost/ ? I'm guessing I'm going to have to put the /var/www/html/cakephp/ folder into a subdirectory /var/www/html/sub/cakephp and then access it at localhost/sub/someController? Not sure this is the right path here or not. – bobby8767 Aug 17 '15 at 19:32

1 Answers1

0

Fix static files first

With the config in the question, what you'll find is that nothing really works - not even requests for static files.

Consider:

server {
    ...

    root /wherever/;
    error_log /tmp/cakephp.err.log debug;  # <- add this

    location /cakephp {
        root /var/www/html/cakephp/app/webroot/;
        index index.php;
        try_files $uri $uri/ /index.php?args;
    }
}

This will produce:

$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 404 Not Found

The debug log will help to clarify why this occurs:

-> cat /tmp/cakephp.err.log 
...
2015/08/23 10:53:43 [debug] 9754#0: *87 http script var: "/cakephp/favicon.ico"
2015/08/23 10:53:43 [debug] 9754#0: *87 trying to use file: "/cakephp/favicon.ico" "/var/www/html/cakephp/app/webroot/cakephp/favicon.ico"

Nginx is using the whole url as the path to a file, not just the bit after the location prefix. This is where understanding the difference between the root directive and the alias directive is important, and is also a common question (random result, there are many).

So, fixing that first:

server {
    ...

    error_log /tmp/cakephp.err.log debug;

    location /cakephp {
        alias /var/www/html/cakephp/app/webroot/; # <- alias, not root
        index index.php;
        try_files $uri $uri/ /index.php?args;
    }
}

Will produce:

$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 200 OK

Then fix php requests

The problem with php requests is more or less the same thing; though the request finds the right php file it's configured such that CakePHP will assume it's installed in the root. There are various solutions to this - here's one:

server {
    ...

    error_log /tmp/cakephp.err.log debug;

    location /cakephp {
        alias /var/www/html/cakephp/app/webroot/;
        index index.php;
        try_files $uri $uri/ /cakephp/index.php; # <- redirect to the actual equivalent request
    }

    location /cakephp/index.php {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;

        # Explicit script filename
        fastcgi_param   SCRIPT_FILENAME /var/www/html/cakephp/app/webroot/index.php;
    }
}

In this way static files and dynamic requests both work - and the environment variables that CakePHP receives are such that it understands the root of the application to be /cakephp/.

Community
  • 1
  • 1
AD7six
  • 63,116
  • 12
  • 91
  • 123