CakePHP和Nginx配置

CakePHP和Nginx配置,cakephp,nginx,Cakephp,Nginx,所以我的任务是使用CakePHP创建一个站点,所以我下载了最新的2.2.3,我需要在本地的nginx 1.2.4服务器上配置它 我的服务器\u块正在工作,但由于某些原因,我无法加载任何css。测试主页还报告url重写工作不正常 我读了很多文章和问题,但似乎没有一篇能够解决这个问题。到目前为止,我已经提到 我的css链接如下所示, 我需要弄清楚为什么Nginx不让我的CSS加载。到目前为止,我只能发现它将$\u服务器['DOCUMENT\u ROOT']附加到我的所有链接,这显然意

所以我的任务是使用CakePHP创建一个站点,所以我下载了最新的2.2.3,我需要在本地的nginx 1.2.4服务器上配置它

我的
服务器\u块
正在工作,但由于某些原因,我无法加载任何css。测试主页还报告url重写工作不正常

我读了很多文章和问题,但似乎没有一篇能够解决这个问题。到目前为止,我已经提到

我的css链接如下所示,

我需要弄清楚为什么Nginx不让我的CSS加载。到目前为止,我只能发现它将
$\u服务器['DOCUMENT\u ROOT']
附加到我的所有链接,这显然意味着我的css将被错误链接。我需要找出谁来阻止cakephp获取这些额外信息。链接被传递到HtmlHelper中,如下所示
string'/Users/david/Sites/example.com//Users/david/Sites/example.com/css/cake.generic.css'(长度=86)

当前,我的nginx config服务器块如下所示

server { 
    server_name  example.com;

    root   /Users/david/Sites/example.com/app/webroot/;

    access_log /usr/local/var/log/nginx/example.com.access.log;
    error_log /usr/local/var/log/nginx/example.com.error.log;

    listen       80;
    rewrite_log on;

    # rewrite rules for cakephp
    location / {
        index  index.php index.html;

        # If the file exists as a static file serve it 
        # directly without running all
        # the other rewite tests on it
        if (-f $request_filename) { 
            break; 
        }

        if (!-f $request_filename) {
            rewrite ^(.+)$ /index.php?url=$1 last;
            break;
        }
    }

    location ~* \favicon.ico$ {
        expires 6m;
    }
    location ~ ^/img/ { 
        expires 7d; 
    } 

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SERVER_NAME example.com;
        include       fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}
server {
    listen   127.0.0.1:80;
    server_name  example.com.ukwm157;

    root /Users/david/Sites/example.com/app/webroot;
    index index.php index.html;

    log_not_found off;
    charset utf-8;

    access_log /usr/local/var/log/nginx/example.com.access.log main;
    error_log /usr/local/var/log/nginx/example.com.error.log;

    location / {

      index index.php index.html index.htm;

      if (-f $request_filename) {
        break;
      }
      if (-d $request_filename) {
        break;
      }

      rewrite ^(.+)$ /index.php?q=$1 last;

    }

    # Static files.
    # Set expire headers, Turn off access log
    location ~* \favicon.ico$ {
      access_log off;
      expires 1d;
      add_header Cache-Control public;
    }

    location ~ ^/(img|cjs|ccss)/ {
      access_log off;
      expires 7d;
      add_header Cache-Control public;
    }

    # Deny access to .htaccess files,
    # git & svn repositories, etc
    location ~ /(\.ht|\.git|\.svn) {
      deny  all;
    }

    location ~ .php?$ {
        #if (!-e $document_root$document_uri){return 404;}
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}
FastCGIParams

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $request_filename;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;

我认为问题在于您使用的是文件路径,而不是URL。

因此我已设法找出问题所在。原来它是
core.php

如果更改
Configure::write('App.baseUrl',env('SCRIPT_NAME')
Configure::write('App.baseUrl','/')然后它似乎都可以正常工作,并将正确路由到您的css文件

我的nginx配置如下所示

server { 
    server_name  example.com;

    root   /Users/david/Sites/example.com/app/webroot/;

    access_log /usr/local/var/log/nginx/example.com.access.log;
    error_log /usr/local/var/log/nginx/example.com.error.log;

    listen       80;
    rewrite_log on;

    # rewrite rules for cakephp
    location / {
        index  index.php index.html;

        # If the file exists as a static file serve it 
        # directly without running all
        # the other rewite tests on it
        if (-f $request_filename) { 
            break; 
        }

        if (!-f $request_filename) {
            rewrite ^(.+)$ /index.php?url=$1 last;
            break;
        }
    }

    location ~* \favicon.ico$ {
        expires 6m;
    }
    location ~ ^/img/ { 
        expires 7d; 
    } 

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SERVER_NAME example.com;
        include       fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}
server {
    listen   127.0.0.1:80;
    server_name  example.com.ukwm157;

    root /Users/david/Sites/example.com/app/webroot;
    index index.php index.html;

    log_not_found off;
    charset utf-8;

    access_log /usr/local/var/log/nginx/example.com.access.log main;
    error_log /usr/local/var/log/nginx/example.com.error.log;

    location / {

      index index.php index.html index.htm;

      if (-f $request_filename) {
        break;
      }
      if (-d $request_filename) {
        break;
      }

      rewrite ^(.+)$ /index.php?q=$1 last;

    }

    # Static files.
    # Set expire headers, Turn off access log
    location ~* \favicon.ico$ {
      access_log off;
      expires 1d;
      add_header Cache-Control public;
    }

    location ~ ^/(img|cjs|ccss)/ {
      access_log off;
      expires 7d;
      add_header Cache-Control public;
    }

    # Deny access to .htaccess files,
    # git & svn repositories, etc
    location ~ /(\.ht|\.git|\.svn) {
      deny  all;
    }

    location ~ .php?$ {
        #if (!-e $document_root$document_uri){return 404;}
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}

以下配置适用于Cake 2.x

server {
listen   80;
server_name myprojecy.xyz;
access_log /var/log/nginx/access.log;
error_log  /var/log/nginx/error.log;

location / {
    root   /var/www/html/project-forlder/app/webroot;
    index  index.php index.html index.htm;

    if (-f $request_filename) {
    break;
    }

    if (-d $request_filename) {
    break;
    }
    rewrite ^(.+)$ /index.php?q=$1 last;
}

location ~ .*\.php[345]?$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index   index.php;
    fastcgi_param SCRIPT_FILENAME
    /var/www/html/project-forlder/app/webroot$fastcgi_script_name;
}

}

具体在哪里?根目录应该是一个文件路径,因为它正在配置服务器上文件的位置。如果你的意思是在CSS中,这是由CakePHP助手处理的,并从服务器获取它的配置。那么你一定是错误地配置了CakePHP,样式表的URL应该是/CSS/cake.generic.CSS或类似的。因此我提出了一个问题。蛋糕实际上还没有配置好。它刚刚被解压缩到一个文件夹中。我想你误解了我的问题。最好的办法是回显/app/webroot/index.php中的
CSS\u URL
,看看值是什么。当你在浏览器中查看页面的源代码并尝试浏览到CSS链接时,会发生什么?(我还假设网页中的链接不会停留在“/Users/dav…”,对吗?@jagsler我得到一个404,因为链接不正确,因为
$\u SERVER['DOCUMENT\u ROOT']
包含绝对路径。问题在于Nginx。