Go(lang)应用程序:nginx反向代理 vs nginx主机

huangapple go评论88阅读模式
英文:

Go(lang) app: nginx reverse proxy VS nginx host

问题

我已经阅读了关于__go__和__nginx__的一些问题,但是我没有找到任何关于我的问题的答案。

我认为(我不是专家)在net/http的go服务器前使用nginx作为反向代理与直接使用nginx托管go应用程序是不同的。
如果我错了,请告诉我,好吗?

我提出这个问题是因为我需要开发一个应用程序(可能使用go,只是为了学习一些新东西),并且对Web服务器有完全的控制,特别是对它用于响应请求的工作线程数量。

所以,以下是我的问题:

  1. 是否可以直接在nginx上托管go应用程序,还是nginx只能提供静态文件服务(如果答案是“否”,那么第二个问题就没有太多意义了)?

  2. 上述两种方法之间的关键区别是什么,具体而言,不同的方法是否会影响配置?

  3. 我担心告诉nginx:“好的,请使用8个工作线程”,而不告诉go的内部Web服务器会发生什么?

非常感谢您的帮助。

英文:

I have already read some of the questions about go and nginx but I didn't find any answer to my one.

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.
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.

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)?

  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?

Thank you so much in advance

答案1

得分: 5

Herbert Fischer编写了一份关于Nginx与Go的综合基准测试,其中包括了Nginx的配置文件和Go代码。

他检查了以下设置:

  • Go HTTP独立运行(作为对照组)
  • Nginx代理到Go HTTP
  • Nginx fastcgi到Go TCP FastCGI
  • Nginx fastcgi到Go Unix Socket FastCGI

Nginx代理到Go HTTP是迄今为止最快的。在Go版本1.2及以后的版本中,结果几乎相同。

英文:

Herbert Fischer wrote a comprehensive benchmark of Nginx with Go, including NGinx configuration files and Go code.

He checked the following settings:

  • Go HTTP standalone (as the control group)
  • Nginx proxy to Go HTTP
  • Nginx fastcgi to Go TCP FastCGI
  • Nginx fastcgi to Go Unix Socket FastCGI

Nginx proxy to Go HTTP was, by far, the fastest. The results were pretty much the same in Go versions since 1.2.

答案2

得分: 3

能直接在nginx上托管Go应用程序吗?

Nginx可以通过各种不同的机制与其后端应用程序进行通信。其中一些机制包括:

  • 通过CGI/FastCGI(进程复用)
  • 通过HTTP(反向代理)
  • 提供由您的应用程序生成的静态文件

不同的方法会对配置产生不同的影响吗?

是的,每种情况都非常不同。

好的,请使用8个worker。

这会建议使用FastCGI,我相信这是您所说的“直接在nginx上托管应用程序”的意思。

如果不告诉Go的内部Web服务器会发生什么?

每个Go FastCGI进程将生成大量的goroutine,这些goroutine被复用到软件线程,然后复用到硬件线程,最后复用到CPU核心。

Go的net/http服务器在生产环境中已经足够好了,除非您想使用一些特定于nginx的功能,否则不一定需要nginx。只有在某些特定情况下,FastCGI设置才有意义。否则,它只会增加开销。

英文:

> Is it possible to directly host a go app on nginx

Nginx can communicate with its backend (your app) through various different mechanisms. Some of them are:

  • Via CGI/FastCGI (process multiplexing)
  • Via HTTP (reverse proxying)
  • 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

That would suggest FastCGI, which I believe is what you mean when you say "directly hosting an application on 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'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. There are only so many use cases where a FastCGI setup makes sense. You just add overhead, basically.

huangapple
  • 本文由 发表于 2015年10月14日 21:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/33126798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定