如何从.yml配置thin以使用tcp套接字?

时间:2017-07-19 17:50:17

标签: ruby-on-rails sockets tcp thin unix-socket

我这里有一个RoR应用程序,我正在使用瘦appserver。

它的配置在.yml文件中,所以:

--- 
pid: /srv/cica/tmp/pids/thin.pid
group: cica
wait: 30
timeout: 30
log: /srv/cica/log/thin.log
max_conns: 1024
require: []

environment: production
max_persistent_conns: 512
servers: 4
daemonize: true
user: cica
socket: /srv/cica/tmp/thin.sock
chdir: /srv/cica

我怎样才能使用TCP套接字而不是unix套接字来监听?

我发现的文档在某种程度上从未提及过这种可能性,尽管间接引用说它是可能的。

问题的原因是前端web(apache2)对于将http请求代理到unix路径不是很强。这对nginx来说不是问题。

1 个答案:

答案 0 :(得分:0)

理论上,您只需使用IP:ADDR而不是套接字路径:

socket: 127.0.0.1:3000

会奏效。但是,如果您使用多个精简流程,则会遇到问题。

(这很可能,因为整个ruby都是一个单线程的东西。考虑到IO等待时间,甚至可能还有一个明显更高的进程数也可以作为CPU内核的数量)。

瘦配置解释器的套接字地址解码器足够聪明地使用普通的IP地址,但是它增加了IP而不是附加套接字的端口。因此,您将有多个精简实例正在侦听

# thin will listen on these addresses
127.0.0.1:3000
127.0.0.2:3000
127.0.0.3:3000
127.0.0.4:3000
相反,他们会听取

# it would be okay, but not this happens
127.0.0.1:3000
127.0.0.1:3001
127.0.0.1:3002
127.0.0.1:3003

这种超现实的行为很可能不是你想要的。 (虽然如果你在所有IP上都有活动接口,它可以工作。)

然而,这个ruby的东西有一个很好的功能,即它的命令行选项和配置文件选项之间有直接的分配。并且thin --help命令会向您显示它们。您可以使用addressport选项强制执行TCP侦听:

#socket: /srv/cica/tmp/thin.sock
address: 127.0.0.1
port: 3000

所以你会得到正确的结果。

默认值为0.0.0.03000

由于apache想要仅使用其最常见的设置(ProxyPassProxyPassReverse指令)来代理单个tcp端口,因此您还需要一些小技巧,即负载平衡代理群集。相关的配置代码段:

<Proxy balancer://cicas>
  BalancerMember http://localhost:3000 disablereuse=On route=cica1
  BalancerMember http://localhost:3001 disablereuse=On route=cica2
  BalancerMember http://localhost:3002 disablereuse=On route=cica3
  BalancerMember http://localhost:3003 disablereuse=On route=cica4
  ProxySet lbmethod=byrequests
</Proxy>

ProxyPass / balancer://cicas/
相关问题