我正在努力让以下设置在我的机器和Heroku上运行:
- 主页是驻留在文档根目录中的PHP文件
- 在CCD_ 1下有一个API。对
/api/*
的所有请求都应转发到其网关文件(api/index.php
)
在本地,我使用了一个不同的conf文件(如下),但在Heroku上,一切都不正常。我能想到的最好的是:
location / {
try_files $uri $uri/ /index.php?$query_string;
index index.php;
}
location ~ ^/api/(.+) {
try_files /api/index.php /api/index.php;
}
location ~ '.php(/|$) {
try_files @heroku-fcgi @heroku-fcgi;
}
如果我尝试使用rewrite,它会抱怨无限循环。如果我尝试将网关脚本设置为index
,并将try_files
与它们的FCGI位置一起使用,我会得到404,因为除了该脚本之外,/api
文件夹下没有任何内容
使用try_files
并直接指向脚本会使Heroku直接发送.php文件进行下载,而不是对其进行解释。如何使其被解释,同时仍然覆盖所有其他/api/*
请求?
Conf文件在我的本地机器上工作:
server {
listen 80;
server_name devshop.dev;
index index.php;
root /home/myself/dev/developer-shop/www/;
location ~ ^/api(/|$) {
try_files $uri $uri/ /api/index.php;
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;
fastcgi_split_path_info ^(.+'.php)(/.*)$;
}
location ~ '.php(/|$) {
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;
fastcgi_split_path_info ^(.+'.php)(/.*)$;
}
}
在本地机器上,PHP脚本在各自的位置块中处理。在目标机器上,/api/
位置执行内部重写,然后期望由php块处理。
Regex位置块被排序,使得root/api
0不断地命中/api/
位置块——因此重定向循环。
或者颠倒regex位置块的顺序,或者更简单地说,使用带有重写的前缀位置块:
location /api {
rewrite ^ /api/index.php;
}
有关详细信息,请参阅本文档。