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

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

问题描述

大家好,请帮帮我,我正在尝试在使用 Fact CGI 运行 Nginx 的 Centos 服务器上设置 cakephp 环境.我已经在服务器上运行了一个 wordpress 站点和一个 phpmyadmin 站点,所以我正确配置了 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.

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

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 正在寻找其要服务的文件,但我私下安装了蛋糕,并在公共链接回/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?

谢谢肖恩

推荐答案

乍一看,您的问题可能是您没有将 nginx 指向应用程序的 webroot.部署到 cake 根文件夹真的不是任何 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(s),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天全站免登陆