Rails/Puma 使用 Apache HTTPD 代理?

Rails/Puma with Apache HTTPD proxy?

我想使用带有 HTTPD 的 Puma 来为我的 Rails 应用程序提供服务。据我所知,人们这样做的例子并不多。我知道 nginx 比 HTTPD 有一些好处,而且 Passenger 使很多事情变得简单,但是有没有理由 使用 Puma/HTTPD?

我已经浏览了一些在线示例并组合了一个我喜欢的 HTTPD 配置块,它似乎工作正常(尽管我没有进行任何性能测试)。我错过了什么吗?这似乎 easier/more 比大多数 Puma/nginx 或 Passenger/HTTPD 设置简单,这让我有点担心。

<VirtualHost *:3008>
  DocumentRoot MY_RAILS_ROOT/public
  ProxyPass /favicon.ico !
  ProxyPass /robots.txt !
  ProxyPassMatch ^/(404|422|500).html$ !
  ProxyPass /assets/ !

  ProxyPass / http://127.0.0.1:9292/ # Puma bind address
  ProxyPassReverse / http://127.0.0.1:9292/
</VirtualHost>

这是 Apache HTTPD 作为反向代理的一种相当常见的用法,因此没有特别的问题。您 运行 Web 服务器和 puma 在同一台机器上吗?拆分它们可能是一个好主意,以防止内存消耗、TCP 堆栈和高负载下可能发生的任何其他争用问题。

如果您有多个 puma 节点,那么您可以使用 HTTPD 在所有节点之间执行负载平衡,如下所示:

<VirtualHost example.org:80>
    ServerName example.org
    ServerAlias www.example.org

    ErrorLog /srv/www/example.org/logs/error.log 
    CustomLog /srv/www/example.org/logs/access.log combined

    DocumentRoot MY_RAILS_ROOT/public

    <Proxy balancer://cluster>
        BalancerMember http://app1.example.org
        BalancerMember http://app2.example.org
    </Proxy>

    ProxyPass /favicon.ico !
    ProxyPass /robots.txt !
    ProxyPassMatch ^/(404|422|500).html$ !
    ProxyPass /assets/ !

    ProxyPass / balancer://cluster/
    # enumerate all nodes for proxypassreverse since it adds a trailing slash :( bugid 51982
    ProxyPassReverse / http://app1.example.org
    ProxyPassReverse / http://app2.example.org


    # ProxyPass / balancer://cluster/ lbmethod=byrequests
    # ProxyPass / balancer://cluster/ lbmethod=bytraffic
    # ProxyPass / balancer://cluster/ lbmethod=bybusyness
</VirtualHost>

尽管如果您有更好的 HA 选项(即 AWS ELB、HAProxy、Varnish 或任何东西),那么我更愿意这样做,因为您可以免费获得第 7 层可用性检查