简体   繁体   English

Go(lang)app:nginx反向代理VS nginx主机

[英]Go(lang) app: nginx reverse proxy VS nginx host

I have already read some of the questions about go and nginx but I didn't find any answer to my one. 我已经阅读了一些关于gonginx问题 ,但我没有找到任何答案。

I think (i'm not an expert) that using nginx as a reverse proxy in front of a net/http go server is different as directly hosting your go application with nginx. 我认为(我不是专家)在net/http go服务器前使用nginx作为反向代理与使用nginx直接托管你的go应用程序是不同的。
Shout at me if I'm wrong, ok? 如果我错了,请大声喊叫,好吗?

The question came to me because I need to develop an application (possibly with go, just to learn something new) and have total control on the webserver, especially on the number of workers it uses to answer requests. 问题出现在我身上,因为我需要开发一个应用程序(可能带有go,只是为了学习新东西)并对Web服务器进行全面控制,尤其是它用于回答请求的工作者数量。

So, here come the questions: 所以,问题来了:

  1. Is it possible to directly host a go app on nginx, or is it nginx that only serves static files (if the answer is "NO", then the second question doesn't make much sense)? 是否有可能直接在nginx上托管一个go应​​用程序,或者它是仅提供静态文件的nginx(如果答案是“否”,那么第二个问题没有多大意义)?

  2. What are the key differences between the two approaches above, precisely, does the different approaches affects the configuration someway? 上述两种方法之间的主要区别是,不同的方法是否会影响配置?

  3. I am scared about telling nginx: "Ok, use 8 workers, please" and telling nothing to go's internal webserver... What would happen? 我害怕告诉nginx:“好的,请使用8名工作人员”,并告诉他们内部的网络服务器......会发生什么?

Thank you so much in advance 非常感谢你提前

Herbert Fischer wrote a comprehensive benchmark of Nginx with Go , including NGinx configuration files and Go code. Herbert Fischer 用Go编写了一个关于Nginx综合基准​​测试 ,包括NGinx配置文件和Go代码。

He checked the following settings: 他检查了以下设置:

  • Go HTTP standalone (as the control group) 单独使用HTTP(作为控制组)
  • Nginx proxy to Go HTTP Nginx代理到Go HTTP
  • Nginx fastcgi to Go TCP FastCGI Nginx fastcgi转到TCP FastCGI
  • Nginx fastcgi to Go Unix Socket FastCGI Nginx fastcgi转到Unix Socket FastCGI

Nginx proxy to Go HTTP was, by far, the fastest. 到目前为止,Go HTTP的Nginx代理是最快的。 The results were pretty much the same in Go versions since 1.2. 自1.2以来,Go版本的结果几乎相同。

Is it possible to directly host a go app on nginx 是否可以在nginx上直接托管go app

Nginx can communicate with its backend (your app) through various different mechanisms. Nginx可以通过各种不同的机制与其后端(您的应用程序)进行通信。 Some of them are: 他们之中有一些是:

  • Via CGI/FastCGI (process multiplexing) 通过CGI / FastCGI(过程多路复用)
  • Via HTTP (reverse proxying) 通过HTTP(反向代理)
  • Serving static files which your app produced 提供应用生成的静态文件

does the different approaches affects the configuration someway? 不同的方法是否会影响配置?

Yes, each case is very different. 是的,每个案例都非常不同。

Ok, use 8 workers, please 好的,请使用8名工人

That would suggest FastCGI, which I believe is what you mean when you say "directly hosting an application on nginx". 这表明FastCGI,我相信当你说“直接在nginx上托管应用程序”时你的意思。

telling nothing to go's internal webserver... What would happen? 什么都不去告诉内部网络服务器......会发生什么?

Each Go FastCGI process would spawn a lot of goroutines, which are multiplexed to software threads, which are multiplexed to hardware threads, which are multiplexed to CPU cores. 每个Go FastCGI进程都会产生许多goroutine,这些goroutine被多路复用到软件线程,软件线程被多路复用到硬件线程,硬件线程被多路复用到CPU内核。

Go's net/http server is good enough for a production environment, you don't necessarily need nginx unless you want to use some nginx-specific feature. Go的net/http服务器对于生产环境来说足够好,除非你想使用一些特定于nginx的功能,否则你不一定需要nginx。 There are only so many use cases where a FastCGI setup makes sense. 只有这么多用例,FastCGI设置才有意义。 You just add overhead, basically. 你只是增加了开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM