简体   繁体   English

转到Webapp和Nginx:关于监听,fastcgi和反向代理的困惑

[英]Go Webapp & Nginx: Confusion about listening, fastcgi & reverse proxy

So I'm trying to create a webapp on Go that accepts all requests from one domain only, with a unique IP, and have all other domains and IPs handled by Nginx (and served with PHP). 因此,我试图在Go上创建一个Webapp,该Webapp仅接受来自一个域的所有请求,并具有唯一的IP,并使所有其他域和IP由Nginx处理(并与PHP一起使用)。

I'm confused about how this is done, it looks like many people are doing this by configuring Nginx to pass requests from a certain domain to FastCGI, which is then listened to from the Go webapp. 我对如何完成此操作感到困惑,似乎很多人正在通过配置Nginx来将请求从特定域传递到FastCGI,然后从Go Webapp进行监听来完成此操作。 Something like this: 像这样:

Nginx.conf Nginx.conf

server_name www.mydomain.com;
listen 123.123.123.123;
include         fastcgi.conf;
fastcgi_pass    127.0.0.1:9001;

Go

func main() {
 listener, _ := net.Listen("tcp", "127.0.0.1:9001")
 srv := new(MyServerObject)
 fcgi.Serve(listener, srv)
}

What I don't understand is this: here we have Nginx listening for a particular domain on a particular IP and then passing that to fastcgi, which then passes it to Go. 我不明白的是:在这里,我们让Nginx侦听特定IP上的特定域,然后将其传递给fastcgi,然后将其传递给Go。

Why do this? 为什么这样 Can I not just have Nginx not listen for this domain, and then Go listen for this domain only? 我不仅可以让Nginx 监听该域,然后再去监听该域吗? Then the same would be achieved more efficiently and without the reverse proxy. 然后,将在没有反向代理的情况下更有效地实现相同的目标。

Since I can use a dedicated IP for the domain that Go is listening to, then there would (in my thinking) be no conflict. 因为我可以将专用IP用于Go所监听的域,所以(在我看来)不会有冲突。 Nginx would simply not be listening to 123.123.123.123:80 and Go would be listening to 123.123.123.123:80... hence no need for the reverse proxy and fastcgi. Nginx根本不会在听123.123.123.123:80,而Go会在听123.123.123.123:80 ...因此不需要反向代理和fastcgi。

Is my thinking on this correct, or am I missing something? 我的想法是否正确,或者我缺少什么?

If it is correct, is all I have to do remove the listener for this IP/domain from Nginx and then use Go code like the below? 如果正确,我是否需要从Nginx中删除该IP /域的侦听器,然后使用如下所示的Go代码?

func main() {
 http.HandleFunc("/", someFunction)
 http.ListenAndServe("123.123.123.123:80", nil)
}

Yes you can have Go listen directly on that IP and it will work fine, there are other uses for nginx, like caching and serving static files. 是的,您可以让Go直接在该IP上侦听,它将正常工作,nginx还有其他用途,例如缓存和提供静态文件。

Also you could have go listen on http.ListenAndServe("127.0.0.1:9020", nil) and proxy from nginx to it: 您也可以在http.ListenAndServe("127.0.0.1:9020", nil)http.ListenAndServe("127.0.0.1:9020", nil)并从nginx代理到它:

server {
        listen 123.123.123.123:80;
        location  /  {
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:9020/;
        }
}

It takes few minutes of testing. 测试需要几分钟。

So ask yourself this, do you need nginx to cache some files or serve static files for you? 所以问问自己,您是否需要nginx缓存某些文件或为您提供静态文件? if yes, use it in front of go, if not, use go directly. 如果是,请在go前面使用它;否则,请直接使用go。

Also if you were to use nginx, the proxy method is probably faster than fcgi. 另外,如果要使用nginx,则代理方法可能比fcgi更快。

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

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