【问题标题】:CakePHP Htaccess 2 Nginx rewriteCakePHP Htaccess 2 Nginx 重写
【发布时间】:2015-02-08 10:44:15
【问题描述】:

我们正在将 CakePHP 框架安装移动到运行 Nginx 的服务器上。以前的服务器有 Apache。这个 CakePHP 在包含 /app/webroot/ 文件夹的子文件夹上有多个子安装。我们已经设法让 index.php 正常工作,但位于 /app/webroot/ 下的所有其他文件(如 javascript 和 CSS)都没有链接到那里。

现在,我们已经尝试让它在具有多种不同变体的 nginx 上工作。问题是,该站点加载了 PHP 文件并清理了 URL 的工作。不加载位于 /app/webroot/ 下的 CSS 和 JS 文件。

我们正在尝试将根目录设置为 subdomain.example.com,其中有一个带有 header() 函数的 index.php,用于将用户重定向到一个文件夹,其中有 CakePHP。基本上子文件夹下的多个站点。所以 CakePHP 网站是http://subdomain.example.com/subfolder

这是我们正在尝试的 nginx 配置。我一直在尝试各种不同的选项,但没有任何效果。

server {
    rewrite ^(.*) http://example.com$1 permanent;
}

server {
    listen         80;
    server_name    example.com www.example.com subdomain.example.com;

    access_log /home/example.com/logs/access.log;
    error_log /home/example.com/logs/error.log error;

    root /home/example.com/public_html/;
    index index.php;

    gzip_static on;

    location /subfolder {
            root /home/example.com/public_html/subfolder/;
            index index.php;

            rewrite ^/subfolder/(/.*)$ /app/webroot$1 break;
            try_files $uri $uri/ /subfolder/app/webroot/index.php?$args;
    }

    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

    location ~ \.php$ {
            try_comles $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/example.com-php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

【问题讨论】:

  • 1. /home/example.com/public_html/cake1/app/webroot/ - 第一个蛋糕,需要打开example.com/cake1//home/example.com/public_html/cake2/app/webroot/ - 第二个蛋糕,需要打开example.com/cake2/ 等等。这是你的文件夹结构吗? 2. images 文件夹在哪里,/home/example.com/public_html/images/(并在安装之间共享)或/home/example.com/public_html/cake2/images//home/example.com/public_html/cake2/app/webroot/images/

标签: apache .htaccess cakephp nginx


【解决方案1】:

app/webroot/ 将是您的服务器根目录。以及进程 index.php 文件的单独位置。

例子:

server {
    listen   80;
    server_name yourserver.com;

    root   /web/path/;
    index  index.php;

    location / {
        rewrite ^(/.*)$ /app/webroot$1 break;
        try_files $uri $uri/ /app/webroot/index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

【讨论】:

  • 您的配置是否完全相同,或者您有其他一些位置/重写规则?如果您不需要处理子目录中的 index.php,您也可以使用 try_files $uri /index.php?$args; - 不使用 $uri/
  • 它适用于您的配置,但我所说的是将其设置在子目录中,而不是将子目录设置为根目录。我们的路径是 example.com/folder,我们的根目录必须是 example.com,其中有一个 index.php 将任何人重定向到正确的文件夹。尝试将根设置为 /web/path/ 然后设置位置 /example { root /web/path/example/app/webroot/;而所有其他设置只会导致网站循环或在 app/webroot 中找不到文件
  • 我改变了答案,试试这个。如果没有 - 请编辑您的问题,并提供更多详细信息:文件夹结构、您从浏览器请求的内容以及您要运行的 php 文件。
  • 是的,对不起。我花了 3 个小时试图调整 conf,甚至向 IRC 上的 CakePHP 开发人员寻求帮助,但没有效果。我调整了我的问题并添加了我们当前正在运行的 nginx conf。
  • 嗯.. 我们实际上通过将符号链接添加到指向 /app/webroot/folder 的 js、img、文件等文件夹的子文件夹根目录中找到了一些解决方案。文件现在可以正常加载了。
猜你喜欢
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-10
相关资源
最近更新 更多