Car*_*nes 9 php api http nginx hhvm
我已经花了很长时间来解决这个问题,而且我无法理解nginx + hhvm如何映射我的请求.
基本上,我在api.example.com上有一个API,我想用Accept:application/vnd.com.example.api.v1 + json调用版本1和application/vnd.com.example.api.v2 + json for version 2. API本身是一个PHP应用程序,我将使用全新安装的HHVM运行.所有请求都将由index.php处理.
api.example.com/
index.php (content: fail)
v1/
index.php (content: v1)
v2/
index.php (content: v2)
Run Code Online (Sandbox Code Playgroud)
每当我使用我的REST客户端访问带有v1接受标头的api.example.com/test时,我都会收到v1响应.当我使用v2的accept标头时,它会显示v2.所以一切都是正确的.如果我不提供任何接受标题,我会被重定向到example.com
map $http_accept $api_version {
default 0;
"application/vnd.com.example.api.v1+json" 1;
"application/vnd.com.example.api.v2+json" 2;
}
server {
# listen to :80 is already implied.
# root directory
root /var/www/api.example.com/;
index index.html;
server_name api.example.com;
include hhvm.conf;
location / {
if ($api_version = 0) {
# redirect to example.com if applicable
# Accept-header is missing
return 307 http://example.com;
}
try_files /v$api_version/$uri /v$api_version/$uri/ /v$api_version/index.php?$args;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
}
Run Code Online (Sandbox Code Playgroud)
hhvm.conf文件包含在下面.它是hhvm中包含的默认hhvm.conf的派生或稍微精确的功能.
location ~ \.(hh|php)$ {
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试访问api.example.com/index.php,即使我期望v1接受标头的v1和v2接受标头的v2,我也会收到"失败"响应.其他一切似乎工作正常,甚至index.html正确地映射到它的子目录.
我试过用了
root /var/www/api.example.com/v$api_version/;
Run Code Online (Sandbox Code Playgroud)
在配置中,但这只给我NGINX的404错误.我相信我正在寻找的实际上是在改变根路径,但我还没有理解如何让它工作.我也尝试在nginx配置和hhvm.conf中删除索引参数,但这似乎没有帮助.我也试过了很多不同的配置,我已经开启了至少20-30个stackoverflow标签来解决这个问题,但我在这里显然遗漏了一些东西(可能相当简单).我也试过在位置块内移动hhvm include.
Debian 7,nginx/1.2.1,hhvm 3.2.0
哦,这是我第一次在这里问一个问题.:)希望我已经正确格式化了一切.
hhvm.conf 的内容是什么?
我假设 Fast CGI 用于将请求代理到 HHVM 服务器。所以你的 hhvm.conf 可能看起来像这样:
root /var/www/api.example.com;
index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
Run Code Online (Sandbox Code Playgroud)
它应该由位置指令包裹。
因此,根据您显示的配置,我认为正在发生的事情是您必须将 php 脚本与 HHVM 位置指令相匹配,这很好,但是这样做时您的 try_files 设置似乎负责将 API 版本写入文件系统映射,尚未处理。
如果没有 hhvm.conf,很难说下一步要做什么,但我怀疑您需要关注包含 HHVM fastcgi 设置的 location 指令内的根值。
更新
因此,我有一个 API 版本的概念,该版本源自映射到在 nginx + HHVM 上为我工作的文件系统的标头。这是我的 HHVM 的 nginx 配置:
location / {
root /var/www/html/hh/v$api_version;
index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)
说实话, 的位置/
并不比你的位置好 - 事实上,你的位置可能更好,因为你可能不希望 HHVM 提供静态文件等。但这对我有用 - 结合你map
在原始帖子中的位置,当我 时,我从版本目录内的文件curl -H 'Accept: application/vnd.com.example.api.v2+json' localhost
中得到预期的响应。index.php
我认为您需要做的是使用root
像上面我的动态生成的声明来更新您的 HHVM nginx 配置。如果仍然出现 404 错误,请尝试以下操作:在 中/etc/init.d/hhvm
,找到ADDITIONAL_ARGS=
var,将其设为ADDITIONAL_ARGS="-vServer.FixPathInfo=true"
。我不确定它到底是做什么的,但我以前遇到过它,它解决了我过去遇到的一个奇怪的 404 问题(404 来自 HHVM,而不是 Apache/nginx)。
归档时间: |
|
查看次数: |
930 次 |
最近记录: |