如何配置nginx重写规则以使CakePHP在CentOS上运行?

时间:2009-06-25 21:51:33

标签: php cakephp nginx rewrite centos

有人请帮助我,我正在尝试使用Fact CGI在运行Nginx的Centos服务器上设置cakephp环境。我已经在服务器上运行了一个wordpress站点和一个phpmyadmin站点,所以我正确配置了PHP。

我的问题是我无法在我的虚拟主机中设置正确的重写规则,以便蛋糕正确呈现页面,即使用样式等等。我已经尽可能多地使用Google搜索,下面列出的网站的主要共识是我需要制定以下重写规则

location / {
          root   /var/www/sites/somedomain.com/current;
          index  index.php index.html;

          # If the file exists as a static file serve it 
          # directly without running all
          # the other rewrite tests on it
          if (-f $request_filename) { 
            break; 
          }
          if (!-f $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
            break;
          }
        }

http://blog.getintheloop.eu/2008/4/17/nginx-engine-x-rewrite-rules-for-cakephp

问题是这些重写假设你直接从webroot运行蛋糕,这不是我想要做的。我为每个站点设置了标准设置,即每个站点包含一个文件夹,其中包含以下文件夹:log,backup,private和public。公共存在nginx正在寻找其服务的文件,但我私下安装了一个带有符号链接的蛋糕,公共链接回/ private / cake /

这是我的vhost

server {
            listen      80;
            server_name app.domain.com;

            access_log /home/public_html/app.domain.com/log/access.log;
            error_log /home/public_html/app.domain.com/log/error.log;

  #configure Cake app to run in a sub-directory
  #Cake install is not in root, but elsewhere and configured
  #in APP/webroot/index.php**

                location /home/public_html/app.domain.com/private/cake {
                index index.php;

    if (!-e $request_filename) {
        rewrite ^/(.+)$ /home/public_html/app.domain.com/private/cake/$1 last;
        break;
    }
}

                location /home/public_html/app.domain.com/private/cake/ {
                index index.php;

    if (!-e $request_filename) {
        rewrite ^/(.+)$ /home/public_html/app.domain.com/public/index.php?url=$1 last;
        break;
        }
}

         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /home/public_html/app.domain.com/private/cake$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
        }

 }

就像我说的那样,我可以看到蛋糕的主要index.php,并将它连接到我的数据库,但这个页面没有样式,所以在我继续进行之前我想正确配置它。我做错了什么?

谢谢seanl

6 个答案:

答案 0 :(得分:9)

乍一看,您的问题可能是您没有将nginx指向应用的webroot。部署到根蛋糕文件夹实际上不是任何网络服务器的方式。

以下是我使用运行Cake应用程序的完整服务器块。实际上我只有前四行,然后从另一个文件“cakephp.inc”中包含其余部分。

“fastcgi_param SERVER_NAME $ host;”行上的注释。这是因为我的一些应用程序使用$ _SERVER ['SERVER_NAME'],它在nginx中与Apache中的含义不同。如果您的服务器有多个server_name(s)已定义,nginx将始终将第一个传递给php。

server { 
    server_name  cakeapp.example.com;
    root   /var/www/vhosts/cake/app/webroot;
    access_log  /var/log/nginx/cakeapp.access.log;
    error_log   /var/log/nginx/cakeapp.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:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SERVER_NAME $host;
    }

    location ~ /\.ht {
        deny  all;
    }
}

答案 1 :(得分:7)

现在有official documentation on this issue,我使用并确认其有效。

文档说明:

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

server {
  listen   80;
  server_name example.com;

  # root directive should be global
  root   /var/www/example.com/public/app/webroot/;
  index  index.php;

  access_log /var/www/example.com/log/access.log;
  error_log /var/www/example.com/log/error.log;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

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

答案 2 :(得分:3)

我有这个工作:

root DIR/app/webroot/;
location / {
    index index.php index.html;
    rewrite ^/$ /index.php?url=/;
    if (!-e $request_filename) {
        rewrite ^(/.*)$ /index.php?url=$1 last;
    }
}

然后当然是php和东西的处理程序......

答案 3 :(得分:2)

不建议使用' IF'在一个位置内的街区'块。

使用正则表达式位置,这是实现相同目标的更自然的方法。

在此示例中,CakePHP 2.x 是vhost上的根应用程序(跳过诸如server_name,日志等常见内容):

 root   /path/to/cakephp-2.x_root/app/webroot;
 index index.php;

 location ~ .+\.php$ {
        try_files $uri =404; #handle requests for missing .php files
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:7001; #the FPM pool port
    }


    location ~ ^/(.*) {
        try_files $uri $uri/ /index.php?url=$1&$args;
    }

请注意.php位置块在/ location块之前。这很重要,因为在正则表达式位置,搜索它们直到第一场比赛。

如果你需要让它在一个子位置运行,例如http://www.example.com/something/,这就是我设法做到的方法。首先我必须做一个符号链接来欺骗nginx:在某处提取cakephp-2.x,然后在app / webroot'使用与子位置相同的名称为自身创建符号链接,例如' ln -s ../webroot something'

然后以下配置可以在/ something /:

下访问cackephp
    location ~ ^/something/.+\.php$ {
        try_files $uri =404; #handle requests for missing .php files
        root /path/to/cakephp-2.x_root/app/webroot;
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:7001; #the FPM pool port
    }

    location ~ ^/something(?:/)(.*) {
        root /path/to/cakephp-2.x_root/app/webroot;
        index index.php;
        try_files $uri $uri/ /something/index.php?url=$1&$args;
    }

使用'别名'可以避免符号化。而不是根源'但我无法弄清楚如何。

答案 4 :(得分:0)

我在设置CakePHP网站时遇到了很多问题,该网站运行的是旧版本的CakePHP 1.2 - 截止到这篇文章的日期可能是时候了。我最近blogged关于它,只是建议升级或安装一个新版本的Cake库,所有问题都消失了。

答案 5 :(得分:0)

请使用以下代码

vi /etc/nginx/sites-available/domainname.com

server { 
server_name  cakeapp.example.com;
root   /var/www/vhosts/cake/app/webroot;
access_log  /var/log/nginx/cakeapp.access.log;
error_log   /var/log/nginx/cakeapp.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:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SERVER_NAME $host;
}

location ~ /\.ht {
    deny  all;
}

}

它为我工作。