子文件夹安装

时间:2015-08-16 23:31:02

标签: cakephp nginx

我试图将cakephp添加到现有服务器上,但正在使用位置/块。我正在关注cakephp食谱上nginx部分的漂亮网址。在我的测试环境中,我的服务器块看起来像

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;
    }
}

由此,我可以通过url localhost / tests

运行我的testsController

但是,我尝试添加cakephp的服务器,已经在域根目录安装了另一个应用程序。

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

我尝试设置像

这样的位置块
location /cakephp {
    root /var/www/html/cakephp/app/webroot/;
    index index.php;
    try_files $uri $uri/ /index.php?args;
}

我知道这不会起作用,因为它在网址中寻找cakephp,它不会在那里。由于root被设置为/ var / www / html / cakephp / app / webroot,当我访问url localhost / cakephp时,它是否在寻找/ var / www / html / cakephp / app / webroot / cakephp?

我对如何设置它感到困惑。我读到了关于url重写和cakephp在某个子目录中运行的内容,但我不确定这是不是我要找的东西。现在,应用程序以http://localhost/someController运行。我想让应用程序与url http://localhost/cakephp/someController一起运行。我该如何设置我的nginx配置?

1 个答案:

答案 0 :(得分:0)

首先修复静态文件

使用问题中的配置,你会发现什么都没有用 - 甚至不是静态文件请求。

考虑:

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;
    }
}

这将产生:

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

调试日志将有助于澄清出现这种情况的原因:

-> 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使用整个url作为文件的路径,而不仅仅是位置前缀之后的位。这是理解root directivealias directive之间差异很重要的地方,也是common question(随机结果,有很多)。

所以,首先解决这个问题:

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;
    }
}

将产生:

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

然后修复php请求

php请求的问题或多或少是相同的;虽然请求找到了正确的php文件,但它的配置使得CakePHP会假定它已安装在根目录中。有各种解决方案 - 这里有一个:

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;
    }
}

通过这种方式,静态文件和动态请求都可以工作 - 而CakePHP接收的环境变量使得它理解应用程序的根目录为/cakephp/