NGINX代理到Cloudant

4

我希望通过使用proxy_pass在我的域名上运行NGINX来展示一些Cloudant的couchdb功能。到目前为止,我已经解决了一些问题(如下所述),但在授权方面卡住了。有人有什么建议吗?

location /couchdb {
    rewrite /couchdb/(.*) /$1 break;   #chop off start of this url

    proxy_redirect off
    proxy_buffering off;
    proxy_set_header Host myusername.cloudant.com;   
    # cannot use $host! must specify my vhost on cloudant

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Authorization "Basic base64-encode(username:password)";

    proxy_pass http://myusername.cloudant.com$request_uri;
    # must use a variable in this url so that the domain is looked up late. 
    # otherwise nginx will fail to start about half the time because of resolver issues
    # (unknown why though)
}

使用这个设置,我可以成功地代理到Cloudant,但我总是收到一个禁止的响应。例如,这个请求:

http://mydomain/couchdb/my-cloudant-db

返回值

{"error":"forbidden", "reason":"_reader access is required for this request"}

感谢任何帮助。


你尝试过自己使用base64编码用户名:密码,而不是使用base64-encode吗? - garbados
我承认我不是nginx方面的专家,但你是否参考过“使用nginx作为反向代理进行身份验证”的文档:http://wiki.apache.org/couchdb/Nginx_As_a_Reverse_Proxy#Authentication_with_reverse_proxy? - garbados
@ garbados 是的,在我的测试nginx.conf中,我将我的用户名:密码base64编码。 我确实从您提到的页面开始设置...我稍微调整了一下,但无法解决被禁止的json错误。 我认为这可能与Cloudant如何处理我的请求有关。 但我不确定,也许我只是缺少其他标题? - DaveEdelstein
2个回答

5
我发现了问题。第一行看似无害的重写规则会重写$request_uri并将$uri变量作为请求执行的一部分进行更改。$request_uri不会被重写。因此,当我在proxy_pass位置中包含该变量时,我没有正确地包含已删除/couchdb/的编辑URL。
将proxy_pass行更改为:
proxy_pass http://myusername.cloudant.com$uri;

现在工作正常。这不是SSL问题,也不是基本认证问题,也不是其他http头问题,也不是Cloudant问题。这一切都与我将请求转发到的URI有关。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接