4.6. 反向代理

4.6.1. 用半氧化法进行反向代理

CouchDB建议使用 HAProxy 作为负载平衡器和反向代理。该团队在生产中使用它的经验表明,它在配置和监视功能以及总体性能方面都非常出色。

CouchDB的haproxy配置示例在 code repository 然后松开防水布 rel/haproxy.cfg . 它包括在下面。此示例适用于3节点CouchDB集群:

global
    maxconn 512
    spread-checks 5

defaults
    mode http
    log global
    monitor-uri /_haproxy_health_check
    option log-health-checks
    option httplog
    balance roundrobin
    option forwardfor
    option redispatch
    retries 4
    option http-server-close
    timeout client 150000
    timeout server 3600000
    timeout connect 500

    stats enable
    stats uri /_haproxy_stats
    # stats auth admin:admin # Uncomment for basic auth

frontend http-in
     # This requires HAProxy 1.5.x
     # bind *:$HAPROXY_PORT
     bind *:5984
     default_backend couchdbs

backend couchdbs
    option httpchk GET /_up
    http-check disable-on-404
    server couchdb1 x.x.x.x:5984 check inter 5s
    server couchdb2 x.x.x.x:5984 check inter 5s
    server couchdb2 x.x.x.x:5984 check inter 5s

4.6.2. 使用nginx进行反向代理

4.6.2.1. 基本配置

下面是一个nginx配置文件的基本摘录 <nginx config directory>/sites-available/default. This will proxy all requests from http://domain.com/...http://localhost:5984/...

location / {
    proxy_pass http://localhost:5984;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

代理缓冲 must 否则在nginx之后连续复制将无法正常工作。

4.6.2.2. 使用nginx在子目录中反向代理CouchDB

将CouchDB作为整个域的子目录是很有用的,特别是为了避免CORS的问题。下面是代理URL的一个基本nginx配置的摘录 http://domain.com/couchdbhttp://localhost:5984 因此,附加到子目录的请求,例如 http://domain.com/couchdb/db1/doc1 委托给 http://localhost:5984/db1/doc1 .

location /couchdb {
    rewrite /couchdb/(.*) /$1 break;
    proxy_pass http://localhost:5984;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

自CouchDB 2.3.0以来,基于会话的复制是默认功能。在代理B中启用基于子目录的反向复制。

location /_session {
    proxy_pass http://localhost:5984/_session;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

4.6.2.3. 使用nginx作为反向代理进行身份验证

下面是一个启用了基本身份验证的配置设置示例,将CouchDB放在 /couchdb 子目录:

location /couchdb {
    auth_basic "Restricted";
    auth_basic_user_file htpasswd;
    rewrite /couchdb/(.*) /$1 break;
    proxy_pass http://localhost:5984;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Authorization "";
}

这个设置完全依赖于nginx执行授权,并将请求转发到CouchDB而不进行身份验证(CouchDB处于管理方模式),这在CouchDB 3.0中是不够的,因为管理方已经被删除。您至少需要将用户凭据硬编码到这个带有头的版本中。

有关更好的解决方案,请参见 代理身份验证 .

4.6.2.4. 带nginx的SSL

要启用SSL,只需启用nginx SSL模块,并添加另一个代理头:

ssl on;
ssl_certificate PATH_TO_YOUR_PUBLIC_KEY.pem;
ssl_certificate_key PATH_TO_YOUR_PRIVATE_KEY.key;
ssl_protocols SSLv3;
ssl_session_cache shared:SSL:1m;

location / {
    proxy_pass http://localhost:5984;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_buffering off;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Ssl on;
}

这个 X-Forwarded-Ssl header告诉CouchDB它应该使用 https 计划而不是 http 计划。否则,所有CouchDB生成的重定向都将失败。

4.6.3. 使用Caddy 2反向代理

凯迪是 https-by-default ,并将在后台自动获取、安装、激活并在必要时为您续订可信SSL证书。证书由 让我们加密 证书颁发机构。

4.6.3.1. 基本配置

这是一个基本的节选 /etc/caddy/Caddyfile. This will proxy all requests from http(s)://domain.com/...http://localhost:5984/...

domain.com {

   reverse_proxy localhost:5984

}

4.6.3.2. 用Caddy 2在子目录中反向代理CouchDB

将CouchDB作为整个域的子目录是很有用的,特别是为了避免CORS的问题。下面是代理URL的基本Caddy配置的摘录 http(s)://domain.com/couchdbhttp://localhost:5984 因此,附加到子目录的请求,例如 http(s)://domain.com/couchdb/db1/doc1 委托给 http://localhost:5984/db1/doc1 .

domain.com {

    reverse_proxy /couchdb/* localhost:5984

}

4.6.3.3. CouchDB集群的反向代理+负载平衡

这是一个基本的节选 /<path>/<to>/<site>/Caddyfile. This will proxy and evenly distribute all requests from http(s)://domain.com/... 在3个CouchDB群集节点中 localhost:15984localhost:25984localhost:35984 .

Caddy将每隔5秒检查每个节点的状态,即运行状况;如果某个节点出现故障,Caddy将避免向该节点发送代理请求,直到该节点恢复联机。

domain.com {

    reverse_proxy http://localhost:15984 http://localhost:25984 http://localhost:35984 {
    lb_policy round_robin
    lb_try_interval 500ms

    health_interval 5s
    }

}

4.6.3.4. 使用Caddy 2作为反向代理进行身份验证

下面是一个启用了基本身份验证的配置设置示例,将CouchDB放在 /couchdb 子目录:

domain.com {

    basicauth /couchdb/* {
        couch_username couchdb_hashed_password_base64
    }

    reverse_proxy /couchdb/* localhost:5984

}

这个设置完全依赖于nginx执行授权,并将请求转发到CouchDB而不进行身份验证(CouchDB处于管理方模式),这在CouchDB 3.0中是不够的,因为管理方已经被删除。您至少需要将用户凭据硬编码到这个带有头的版本中。

有关更好的解决方案,请参见 代理身份验证 .

4.6.4. 使用Apache HTTP服务器进行反向代理

警告

在撰写本文时,还没有办法完全禁用apachehttpd服务器和CouchDB之间的缓冲。这可能会导致连续复制出现问题。apachecouchdb团队强烈建议使用替代的反向代理,例如 haproxynginx ,如本节前面所述。

4.6.4.1. 基本配置

下面是使用 VirtualHost 使用Apache作为CouchDB的反向代理。您至少需要使用 --enable-proxy --enable-proxy-http 选项并使用等于或高于Apache 2.2.7的版本,以便使用 nocanon 期权在 ProxyPass 指令。这个 ProxyPass 指令添加 X-Forwarded-For CouchDB需要的头文件,以及 ProxyPreserveHost 指令确保原始客户端 Host 标题被保留。

<VirtualHost *:80>
   ServerAdmin webmaster@dummy-host.example.com
   DocumentRoot "/opt/websites/web/www/dummy"
   ServerName couchdb.localhost
   AllowEncodedSlashes On
   ProxyRequests Off
   KeepAlive Off
   <Proxy *>
      Order deny,allow
      Deny from all
      Allow from 127.0.0.1
   </Proxy>
   ProxyPass / http://localhost:5984 nocanon
   ProxyPassReverse / http://localhost:5984
   ProxyPreserveHost On
   ErrorLog "logs/couchdb.localhost-error_log"
   CustomLog "logs/couchdb.localhost-access_log" common
</VirtualHost>