【问题标题】:Subfolder installation子文件夹安装
【发布时间】:2015-08-16 23:31:01
【问题描述】:

我正在尝试将 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 运行我的测试控制器

但是,我尝试将 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;
}

我知道这行不通,因为它在 url 中寻找 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 配置?

【问题讨论】:

  • @AD7six 访问 url localhost/cakephp,它说没有找到 CakephpController。我创建了控制器并且能够访问正确的网页。这意味着我必须为每个控制器创建一个新的位置块?嗯...有没有办法将 url 设置为使用 localhost/cakephp 而不是 localhost/ ?我猜我将不得不将 /var/www/html/cakephp/ 文件夹放入子目录 /var/www/html/sub/cakephp ,然后在 localhost/sub/someController 中访问它?不确定这是否是正确的路径。

标签: cakephp nginx


【解决方案1】:

先修复静态文件

使用问题中的配置,您会发现没有什么真正有效 - 甚至对静态文件的请求也不行。

考虑:

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多