子文件夹安装
Subfolder installation
我正在尝试将 cakephp 添加到现有服务器上,但位置/块正在使用中。我正在关注 cakephp cookbook 上关于 nginx 的漂亮 url 部分。在我的测试环境中,服务器块看起来像
server {
listen 80;
server_name localhost;
access_log /var/www/html/log/access.log;
error_log /var/www/html/log/error.log;
location / {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
由此,我能够 运行 我的 testsController 通过 url localhost/tests
但是,我尝试将 cakephp 添加到的服务器的域根目录中已经安装了另一个应用程序。
location / {
proxy_pass https://localhost/somepage;
}
我尝试设置一个位置块,例如
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
我知道这行不通,因为它正在 url 中寻找 cakephp,而它不会在那里。由于root设置为/var/www/html/cakephp/app/webroot,当我访问url localhost/cakephp时,是否在寻找/var/www/html/cakephp/app/webroot/cakephp?
我对如何设置感到困惑。我在某些子目录中读到 url 重写和 cakephp 运行ning,但我不确定这是否是我要找的。现在,应用程序 运行 具有 http://localhost/someController. I would like to have the the application run with the url http://localhost/cakephp/someController。我应该如何设置我的 nginx 配置?
先修复静态文件
使用问题中的配置,您会发现没有任何实际效果 - 甚至对静态文件的请求也不行。
考虑:
server {
...
root /wherever/;
error_log /tmp/cakephp.err.log debug; # <- add this
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
这将产生:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 404 Not Found
调试日志将有助于阐明发生这种情况的原因:
-> cat /tmp/cakephp.err.log
...
2015/08/23 10:53:43 [debug] 9754#0: *87 http script var: "/cakephp/favicon.ico"
2015/08/23 10:53:43 [debug] 9754#0: *87 trying to use file: "/cakephp/favicon.ico" "/var/www/html/cakephp/app/webroot/cakephp/favicon.ico"
Nginx 使用整个 url 作为文件路径,而不仅仅是位置前缀后的位。这就是理解root directive and the alias directive is important, and is also a common question(随机结果,有很多)的区别所在。
所以,首先解决这个问题:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/; # <- alias, not root
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
将产生:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 200 OK
然后修复 php 个请求
php 请求的问题大致相同;尽管请求找到了正确的 php 文件,但它的配置使得 CakePHP 将假定它安装在根目录中。有多种解决方案 - 这是一个:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /cakephp/index.php; # <- redirect to the actual equivalent request
}
location /cakephp/index.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# Explicit script filename
fastcgi_param SCRIPT_FILENAME /var/www/html/cakephp/app/webroot/index.php;
}
}
通过这种方式,静态文件和动态请求都可以工作 - CakePHP 接收到的环境变量使得它可以将应用程序的根理解为 /cakephp/
。
我正在尝试将 cakephp 添加到现有服务器上,但位置/块正在使用中。我正在关注 cakephp cookbook 上关于 nginx 的漂亮 url 部分。在我的测试环境中,服务器块看起来像
server {
listen 80;
server_name localhost;
access_log /var/www/html/log/access.log;
error_log /var/www/html/log/error.log;
location / {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
由此,我能够 运行 我的 testsController 通过 url localhost/tests
但是,我尝试将 cakephp 添加到的服务器的域根目录中已经安装了另一个应用程序。
location / {
proxy_pass https://localhost/somepage;
}
我尝试设置一个位置块,例如
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
我知道这行不通,因为它正在 url 中寻找 cakephp,而它不会在那里。由于root设置为/var/www/html/cakephp/app/webroot,当我访问url localhost/cakephp时,是否在寻找/var/www/html/cakephp/app/webroot/cakephp?
我对如何设置感到困惑。我在某些子目录中读到 url 重写和 cakephp 运行ning,但我不确定这是否是我要找的。现在,应用程序 运行 具有 http://localhost/someController. I would like to have the the application run with the url http://localhost/cakephp/someController。我应该如何设置我的 nginx 配置?
先修复静态文件
使用问题中的配置,您会发现没有任何实际效果 - 甚至对静态文件的请求也不行。
考虑:
server {
...
root /wherever/;
error_log /tmp/cakephp.err.log debug; # <- add this
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
这将产生:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 404 Not Found
调试日志将有助于阐明发生这种情况的原因:
-> cat /tmp/cakephp.err.log
...
2015/08/23 10:53:43 [debug] 9754#0: *87 http script var: "/cakephp/favicon.ico"
2015/08/23 10:53:43 [debug] 9754#0: *87 trying to use file: "/cakephp/favicon.ico" "/var/www/html/cakephp/app/webroot/cakephp/favicon.ico"
Nginx 使用整个 url 作为文件路径,而不仅仅是位置前缀后的位。这就是理解root directive and the alias directive is important, and is also a common question(随机结果,有很多)的区别所在。
所以,首先解决这个问题:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/; # <- alias, not root
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
将产生:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 200 OK
然后修复 php 个请求
php 请求的问题大致相同;尽管请求找到了正确的 php 文件,但它的配置使得 CakePHP 将假定它安装在根目录中。有多种解决方案 - 这是一个:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /cakephp/index.php; # <- redirect to the actual equivalent request
}
location /cakephp/index.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# Explicit script filename
fastcgi_param SCRIPT_FILENAME /var/www/html/cakephp/app/webroot/index.php;
}
}
通过这种方式,静态文件和动态请求都可以工作 - CakePHP 接收到的环境变量使得它可以将应用程序的根理解为 /cakephp/
。