简体   繁体   English

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

[英]How do I configure nginx rewrite rules to get CakePHP working on CentOS?

Hi somebody please help me out, I'm trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. 嗨,有人请帮助我,我正在尝试使用Fact CGI在运行Nginx的Centos服务器上设置cakephp环境。 I already have a wordpress site running on the server and a phpmyadmin site so I have PHP configured correctly. 我已经在服务器上运行了一个wordpress站点和一个phpmyadmin站点,所以我正确配置了PHP。

My problem is that I cannot get the rewrite rules setup correct in my vhost so that cake renders pages correctly ie with styling and so on. 我的问题是我无法在我的vhost中正确设置重写规则,以便蛋糕正确呈现页面,即使用样式等。 I've googled as much as possible and the main consensus from the sites like the one listed below is that I need to have the following rewrite rule in place 我已经尽可能多地使用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 http://blog.getintheloop.eu/2008/4/17/nginx-engine-x-rewrite-rules-for-cakephp

problem is these rewrite assume you run cake directly out of the webroot which is not what I want to do. 问题是这些重写假设您直接从webroot运行蛋糕,这不是我想要做的。 I have a standard setup for each site ie one folder per site containing the following folders log, backup, private and public. 我为每个站点设置了标准设置,即每个站点包含一个文件夹,其中包含以下文件夹日志,备份,私有和公共。 Public being where nginx is looking for its files to serve but I have cake installed in private with a symlink in public linking back to /private/cake/ 公共存在nginx正在寻找其服务的文件,但我私下安装了蛋糕,公共链接回到/ private / cake /

this is my vhost 这是我的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;
        }

 }

Now like I said I can see the main index.php of cake and have connected it to my DB but this page is without styling so before I proceed any further I would like to configure it correctly. 就像我说的那样,我可以看到蛋糕的主要index.php并将它连接到我的数据库,但是这个页面没有样式,所以在我继续进行之前我想正确配置它。 What am I doing wrong? 我究竟做错了什么?

Thanks seanl 谢谢seanl

At a glance, your problem might be that you are not pointing nginx to the webroot of your app. 一目了然,您的问题可能是您没有将nginx指向应用程序的webroot。 Deploying to the root cake folder is really not the way to go under any web-server. 部署到根蛋糕文件夹实际上不是任何Web服务器的方式。

The following is a complete server-block I use running Cake apps. 以下是我使用运行Cake应用程序的完整服务器块。 In reality I only have the first four lines and then include the rest from a separate file "cakephp.inc". 实际上我只有前四行,然后从单独的文件“cakephp.inc”中包含其余部分。

A note on the line "fastcgi_param SERVER_NAME $host;". “fastcgi_param SERVER_NAME $ host;”行上的注释。 This is because some of my apps use $_SERVER['SERVER_NAME'] and it does not have the same meaning in nginx as in Apache. 这是因为我的一些应用程序使用$ _SERVER ['SERVER_NAME'],它在nginx中与Apache中的含义不同。 If youe server has several server_name(s) defined nginx will always pass the first one to php. 如果您的服务器有多个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;
    }
}

There's now official documentation on this issue , which I used and confirmed works. 现在有关于这个问题的官方文档 ,我使用并确认了它的作用。

The documentation states: 文件说明:

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

I got this working: 我有这个工作:

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

and then of course handlers for php and stuff... 当然还有php和东西的处理程序......

It is not advisable to use 'IF' blocks inside a 'location' block. 不建议在“位置”块内使用“IF”块。

Here is a more natural way to achieve the same, using regex locations. 使用正则表达式位置,这是实现相同目标的更自然的方法。

In this example, CakePHP 2.x is the root app on a vhost (skipping common stuff like server_name , logs etc): 在此示例中,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;
    }

Note that the .php location block is BEFORE the / location block. 请注意,.php位置块位于/位置块之前。 That's important because with regex locations, they are searched until the first match. 这很重要,因为在正则表达式位置,搜索它们直到第一次匹配。

If you need to make it run in a sublocation, eg http://www.example.com/something/ , here is how I managed to do it. 如果你需要让它在一个子位置运行,例如http://www.example.com/something/ ,这就是我设法做到的方法。 First I had to do a symlink to trick nginx: extract cakephp-2.x somewhere, then in 'app/webroot' create a symlink to itself with the same name as the sublocation, eg 'ln -s ../webroot something' . 首先,我必须做一个符号链接来欺骗nginx:在某处提取cakephp-2.x,然后在'app / webroot'中创建一个与子位置同名的符号链接,例如'ln -s ../webroot something' 。

Then the following config works to access cackephp under /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;
    }

Symlinking can probably be avoided by using 'alias' istead of 'root' but I could not figure out how. 使用'别名'而不是'root'可能可以避免符号化,但我无法弄清楚如何。

I had a bunch of issues setting up a CakePHP site that was running an older version of CakePHP 1.2 - going by the date of this post it might be around the time. 我有一堆问题设置了一个CakePHP网站,该网站运行的是旧版本的CakePHP 1.2 - 从这篇文章发布之日起,它可能就在时间之前。 I recently blogged about it and simply suggest upgrading or installing a fresh version of the Cake library and all problems went away. 我最近在博客上发表了关于它的文章,并建议升级或安装一个新版本的Cake库,所有问题都消失了。

Please use below code in 请使用以下代码

vi /etc/nginx/sites-available/domainname.com 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;
}

} }

Its working for me. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM