新手问题 Nginx + Unicorn 部署配置问题

匿名 · 2013年07月02日 · 最后由 yunzifeiyu 回复于 2013年07月03日 · 6106 次阅读

我在学习搭建 ruby on rails 环境,用的是社区里讨论很多的 Nginx + Unicorn 部署。 对于 Nginx 和 Unicorn 的配置有稍许疑问,希望能有学长帮忙解答。

nginx 配置: upstream ruby_backend { server unix:/tmp/unicorn.sock fail_timeout=0; }

Unicorn 配置: listen 5000, :tcp_nopush => false

以上配置时侦听 5000 端口,网站可以正常解析,但是把配置改为: Unicorn 配置: listen“/tmp/unicorn.sock”, :tcp_nopush => false 也可以正常解析。

疑问: ①是不是可以说在 Unicorn 配置中“listen 5000”的配置可写可不写。nginx 只是通过 unicorn 生成的 unicorn.sock 文件来 upstream 数据,并不是通过 listen 的 5000 端口进行的数据通信。

②刚才看见一篇蝉游记网站的部署(topics/12033),其中对 unicorn 的配置中有如下配置: a).listen "/tmp/unicorn.sock", :backlog => 64 b).listen 4096, :tcp_nopush => false 请问 a,b 两句配置各起到什么作用,有什么区别?

匿名 #1 2013年07月02日

刚才看了 Unicorn 官网关于 listen 的说明(http://unicorn.bogomips.org/Unicorn/Configurator.html) When using Unix domain sockets, be sure: 1) the path matches the one used by nginx 2) uses the same filesystem namespace as the nginx process For systemd users using PrivateTmp=true (for either nginx or unicorn), this means Unix domain sockets must not be placed in /tmp

但是我看很多人文章都是吧 unicorn.sock 文件都放在了/tmp 下面,这个有什么特殊要注意的?

匿名 #2 2013年07月02日

:tcp_nopush => true or false Enables/disables TCP_CORK in Linux or TCP_NOPUSH in FreeBSD This prevents partial TCP frames from being sent out and reduces wakeups in nginx if it is on a different machine. Since Unicorn is only designed for applications that send the response body quickly without keepalive, sockets will always be flushed on close to prevent delays. This has no effect on UNIX sockets. Default: false This defaulted to true in Unicorn 3.4 - 3.7

也就是说 listen 对于 unix socket 方式进行连携的配置没有影响。

:backlog => number of clients This is the backlog of the listen() syscall. Some operating systems allow negative values here to specify the maximum allowable value. In most cases, this number is only recommendation and there are other OS-specific tunables and variables that can affect this number. See the listen(2) syscall documentation of your OS for the exact semantics of this. If you are running unicorn on multiple machines, lowering this number can help your load balancer detect when a machine is overloaded and give requests to a different machine. Default: 1024

次配置只是针对于多台服务器之间的负载均衡,单服务器不造成影响?

鉴于以上对 listen 的理解,是否可以说 unix socket 方式部署的时候 listen 的配置不是 ruby 解析 nginx 转发来的请求所必须的配置?

Unicorn 是我心中永远的痛 当初死活也 run 不起来

我们是有 3 台机器提供 web 服务,每台上都运行了 nginx 和 unicorn,unicorn 同时 listen domain socket 和 tcp socket。每台的 nginx 都是 proxy 到本机的 domain socket 和其他另外 2 台机器的 tcp socket。所以同时配置了 2 个 lisetn。

这样配置目的是万一有一台挂了,可以将 ip 切换到另外 2 台中的一台提供对外服务。

匿名 #5 2013年07月03日

#4 楼 @quakewang 多谢多谢,恍然大悟啊,呵呵

需要 登录 后方可回复, 如果你还没有账号请 注册新账号