Mongodb REST API returns 没有结果
Mongodb REST API returns no results
我正在使用内置 MongoDB "simple REST interface" 尝试将我的 MongoDB collection 与一个简单的 javascript 前端联系在一起,而无需编写一个 API 我自己。我已经启动了 REST 服务器并且 运行,运行 我的生产服务器中的以下内容证明了这一点:
$> http get "http://localhost:28017/mydb/mycoll/?limit=1"
HTTP/1.0 200 OK
Connection: close
Content-Length: 437
Content-Type: text/plain;charset=utf-8
x-action:
x-ns: mydb.mycoll
{
"offset" : 0,
"rows": [
{ <redacted data> }
],
"total_rows" : 1 ,
"query" : {} ,
"millis" : 0
}
默认情况下,Mongodb REST 服务器不会绑定到可公开访问的接口,因此我使用 Nginx 将 localhost:28017 代理到外部世界,如下所示配置:
server {
listen 80;
server_name api.myapp.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 default_server;
server_name api.myapp.com;
ssl on;
ssl_certificate /etc/ssl/myapp.crt;
ssl_certificate_key /etc/ssl/myapp.key;
access_log /logs/nginx_access.log;
error_log /logs/nginx_error.log;
location /mongo {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:28017;
break;
}
}
}
我重新加载 nginx 并尝试在托管 Mongo 的机器内 运行 从该网络外的机器尝试相同的查询,并得到:
$> http get "https://api.myapp.com/mongo/mydb/mycoll/?limit=1"
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/plain;charset=utf-8
Date: Fri, 27 Feb 2015 17:06:40 GMT
Server: nginx/1.4.6 (Ubuntu)
Transfer-Encoding: chunked
Vary: Accept-Encoding
x-action:
x-ns: mongo.mydb.mycoll
{
"offset" : 0,
"rows": [
],
"total_rows" : 0 ,
"query" : {} ,
"millis" : 0
}
本质上,它 是 命中 Mongo REST 服务器(通过 nginx 代理),但它没有为我的同一个查询返回任何结果运行 在本地主机上。
到目前为止我唯一的线索是两个响应中的 x-ns
header 是不同的——就像 Mongo REST 服务器没有意识到 Nginx 是指示它位于 https://api.myapp.com/mongo
路线后面,而是认为我正在尝试访问数据库 mongo
.
感谢您的帮助。
解决了我自己的问题。问题正如我在问题末尾所猜测的那样:Nginx 配置正在代理对 Mongo API 服务器的请求,并且 /mongo/
路径完好无损,导致查询 运行 在(不存在的)数据库 mongo
.
我通过将以下行添加到 Nginx 配置文件中的 /mongo/
位置来解决问题:
rewrite /mongo/(.+) / break;
所以它的全文是:
location /mongo/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
rewrite /mongo/(.+) / break;
proxy_pass http://127.0.0.1:28017;
break;
}
}
感谢任何看过这个问题的人。
我正在使用内置 MongoDB "simple REST interface" 尝试将我的 MongoDB collection 与一个简单的 javascript 前端联系在一起,而无需编写一个 API 我自己。我已经启动了 REST 服务器并且 运行,运行 我的生产服务器中的以下内容证明了这一点:
$> http get "http://localhost:28017/mydb/mycoll/?limit=1"
HTTP/1.0 200 OK
Connection: close
Content-Length: 437
Content-Type: text/plain;charset=utf-8
x-action:
x-ns: mydb.mycoll
{
"offset" : 0,
"rows": [
{ <redacted data> }
],
"total_rows" : 1 ,
"query" : {} ,
"millis" : 0
}
默认情况下,Mongodb REST 服务器不会绑定到可公开访问的接口,因此我使用 Nginx 将 localhost:28017 代理到外部世界,如下所示配置:
server {
listen 80;
server_name api.myapp.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 default_server;
server_name api.myapp.com;
ssl on;
ssl_certificate /etc/ssl/myapp.crt;
ssl_certificate_key /etc/ssl/myapp.key;
access_log /logs/nginx_access.log;
error_log /logs/nginx_error.log;
location /mongo {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:28017;
break;
}
}
}
我重新加载 nginx 并尝试在托管 Mongo 的机器内 运行 从该网络外的机器尝试相同的查询,并得到:
$> http get "https://api.myapp.com/mongo/mydb/mycoll/?limit=1"
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/plain;charset=utf-8
Date: Fri, 27 Feb 2015 17:06:40 GMT
Server: nginx/1.4.6 (Ubuntu)
Transfer-Encoding: chunked
Vary: Accept-Encoding
x-action:
x-ns: mongo.mydb.mycoll
{
"offset" : 0,
"rows": [
],
"total_rows" : 0 ,
"query" : {} ,
"millis" : 0
}
本质上,它 是 命中 Mongo REST 服务器(通过 nginx 代理),但它没有为我的同一个查询返回任何结果运行 在本地主机上。
到目前为止我唯一的线索是两个响应中的 x-ns
header 是不同的——就像 Mongo REST 服务器没有意识到 Nginx 是指示它位于 https://api.myapp.com/mongo
路线后面,而是认为我正在尝试访问数据库 mongo
.
感谢您的帮助。
解决了我自己的问题。问题正如我在问题末尾所猜测的那样:Nginx 配置正在代理对 Mongo API 服务器的请求,并且 /mongo/
路径完好无损,导致查询 运行 在(不存在的)数据库 mongo
.
我通过将以下行添加到 Nginx 配置文件中的 /mongo/
位置来解决问题:
rewrite /mongo/(.+) / break;
所以它的全文是:
location /mongo/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
rewrite /mongo/(.+) / break;
proxy_pass http://127.0.0.1:28017;
break;
}
}
感谢任何看过这个问题的人。