我正在尝试在 Ubuntu Server 12.04 实例上使用 Apache 设置 CouchDB 的反向代理。我一直通过端口 5984 直接访问 CouchDB,但响应缺乏压缩导致网络使用过多,反向代理允许我启用 gzip 压缩。
到目前为止,我已经为我的站点设置了配置文件:
# /etc/apache2/sites-available/SITE_NAME
<VirtualHost *:80>
ServerAdmin *****
ServerName *****
ServerAlias *****
RewriteEngine On
RewriteOptions Inherit
RewriteRule ^/couch/(.*) http://%{HTTP_HOST}:5984/$1 [P]
ProxyPass /couch/ http://localhost:5984/ nocanon
ProxyPassReverse /couch/ http://localhost:5984/
DocumentRoot /var/www/*****/current/public
<Directory /var/www/*****/current/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
此时,我可以使用 curl 通过反向代理访问 CouchDB,例如
curl -X GET http://MY_SERVER_ADDRESS/couch/_all_dbs
我每次都能得到成功的回应。
但是,此代理的主要客户端是 iOS 设备,每当我使用 NSURLConnection
运行相同的请求时,我都会收到 502 Bad Gateway
错误。有了这个回应:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /couch/DATABASE_NAME/_changes.Reason: DNS lookup failure for: MY_SERVER_ADDRESS
Apache/2.2.22 (Ubuntu) Server at MY_SERVER_ADDRESS Port 80
我已经将 NSURLRequest
中的 exact URL 和 header 字段复制并粘贴到 curl 请求中,但我无法复制错误终点站。我也试过 changing timeout values在配置文件中,但没有区别。
注意这既发生在本地网络上的 iOS 设备上,也发生在与服务器运行在同一台机器上的 iOS 模拟器中(服务器是在 Virtual Box 中运行的虚拟机)。
最佳答案
通常,我在发布问题后几乎立即找到了答案。
这个问题的解决方案是从 NSURLRequest
传递一个 Host
header ,其中包含服务器地址的值。例如:
Host: 10.0.0.1
或者对于NSURLRequest
:
[request addValue:[url host] forHTTPHeaderField:@"Host"];
关于ios - Apache 向 CouchDB 报告反向代理上的 502 错误网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19005381/