当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何配置nginx重写规则以使CakePHP在CentOS上运行?》经验,为你挑选了2个好方法。

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

我的问题是我无法在我的vhost中正确设置重写规则,以便蛋糕正确呈现页面,即使用样式等.我已经尽可能多地使用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运行蛋糕,这不是我想要做的.我为每个站点设置了标准设置,即每个站点包含一个文件夹,其中包含以下文件夹日志,备份,私有和公共.公共存在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



1> Martin Westi..:

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

以下是我使用运行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;
    }
}



2> 小智..:

现在有关于这个问题的官方文档,我使用并确认了它的作用.

文件说明:

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

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有