php 如何配置 nginx 重写规则以使 CakePHP 在 CentOS 上工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1046439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure nginx rewrite rules to get CakePHP working on CentOS?
提问by
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.
嗨,有人请帮助我,我正在尝试在运行 Nginx 和 Fact CGI 的 Centos 服务器上设置 cakephp 环境。我已经在服务器上运行了一个 wordpress 站点和一个 phpmyadmin 站点,所以我已经正确配置了 PHP。
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= 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. 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/
问题是这些重写假设您直接从 webroot 中运行 cake 这不是我想要做的。我为每个站点都有一个标准设置,即每个站点一个文件夹,其中包含以下文件夹日志、备份、私有和公共。公开是 nginx 正在寻找要提供的文件的地方,但我私下安装了蛋糕,并在公共链接中使用符号链接返回 /private/cake/
this is my 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/ 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= 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. What am I doing wrong?
现在就像我说的,我可以看到 cake 的主要 index.php 并将它连接到我的数据库,但是这个页面没有样式,所以在我继续之前我想正确配置它。我究竟做错了什么?
Thanks seanl
谢谢seanl
回答by Martin Westin
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.
乍一看,您的问题可能是您没有将 nginx 指向应用程序的 webroot。部署到根 cake 文件夹确实不是在任何网络服务器下进行的方式。
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".
以下是我用来运行 Cake 应用程序的完整服务器块。实际上,我只有前四行,然后将其余部分包含在单独的文件“cakephp.inc”中。
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.
“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= 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;
}
}
回答by diginc
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 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;
}
}
回答by Marco Romeny
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= last;
}
}
and then of course handlers for php and stuff...
然后当然是php和东西的处理程序......
回答by pumpal
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.xis the root app on a vhost (skipping common stuff like server_name , logs etc):
在这个例子中,CakePHP 2.x是虚拟主机上的根应用程序(跳过了 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=&$args;
}
Note that the .php location block is BEFORE the / location block. That's important because with regex locations, they are searched until the first match.
请注意,.php 位置块位于 / location 块之前。这很重要,因为对于正则表达式位置,它们会被搜索到第一个匹配项。
If you need to make it run in a sublocation, eg http://www.example.com/something/, here is how I managed to do it. 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, e.g. 'ln -s ../webroot something' .
如果您需要让它在子位置运行,例如http://www.example.com/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=&$args;
}
Symlinking can probably be avoided by using 'alias' istead of 'root' but I could not figure out how.
符号链接可能可以通过使用“别名”而不是“根”来避免,但我不知道如何。
回答by endyourif
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. I recently bloggedabout it and simply suggest upgrading or installing a fresh version of the Cake library and all problems went away.
我在设置运行旧版本 CakePHP 1.2 的 CakePHP 站点时遇到了很多问题 - 到本文发布之日可能已经差不多了。我最近写了一篇关于它的博客,并简单地建议升级或安装 Cake 库的新版本,所有问题都消失了。
回答by Mayur Godhani
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= 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.
它为我工作。

