如何配置nginx重写规则以使CakePHP在CentOS上运行? [英] How do I configure nginx rewrite rules to get CakePHP working on CentOS?

查看:169
本文介绍了如何配置nginx重写规则以使CakePHP在CentOS上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,请帮帮我,我正在尝试在运行带有Fact CGI的Nginx的Centos服务器上设置cakephp环境.我已经在服务器和phpmyadmin站点上运行了一个wordpress站点,因此我已正确配置了PHP.

Hi somebody please help me out, I’m trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. I already have a wordpress site running on the server and a phpmyadmin site so I have PHP configured correctly.

我的问题是我无法在我的虚拟主机中正确设置重写规则,因此蛋糕无法正确呈现页面,即具有样式等功能.我已经在Google上进行了尽可能多的搜索,以下列出的网站的主要共识是,我需要制定以下重写规则

My problem is that I cannot get the rewrite rules setup correct in my vhost so that cake renders pages correctly i.e. with styling and so on. 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

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之外运行cake,这不是我想要做的.我为每个站点都有一个标准设置,即每个站点一个文件夹,其中包含以下文件夹日志,备份,私有和公共文件夹. nginx正在寻找要提供其文件的公共场所,但是我在私有状态下安装了Cake,并在公共环境中建立了一个符号链接,链接回/private/cake/

problem is these rewrite assume you run cake directly out of the webroot which is not what I want to do. I have a standard setup for each site i.e. 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/

这是我的虚拟主机

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并将其连接到我的数据库,但是此页面没有样式设置,因此在继续进行操作之前,我想对其进行正确配置.我在做什么错了?

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. What am I doing wrong?

感谢seanl

推荐答案

乍一看,您的问题可能是您没有将nginx指向应用程序的webroot.部署到根蛋糕文件夹实际上不是在任何Web服务器下进行的方式.

At a glance, your problem might be that you are not pointing nginx to the webroot of your app. Deploying to the root cake folder is really not the way to go under any web-server.

以下是我使用正在运行的Cake应用程序的完整服务器块.实际上,我只有前四行,然后将其余部分包含在单独的文件"cakephp.inc"中.

The following is a complete server-block I use running Cake apps. In reality I only have the first four lines and then include the rest from a separate file "cakephp.inc".

"fastcgi_param SERVER_NAME $ host;"行上的注释.这是因为我的某些应用程序使用$ _SERVER ['SERVER_NAME'],并且在nginx中的含义与在Apache中的含义不同.如果您的服务器定义了多个server_name,nginx总是将第一个传递给php.

A note on the line "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. If youe server has several server_name(s) defined nginx will always pass the first one to 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;
    }
}

这篇关于如何配置nginx重写规则以使CakePHP在CentOS上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆