Go Webapp&Nginx:关于监听、fastcgi和反向代理的困惑

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

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

问题

所以我正在尝试在Go上创建一个Web应用程序,只接受来自一个特定域名和唯一IP的所有请求,并且将所有其他域名和IP交给Nginx处理(并使用PHP提供服务)。

我对如何实现这一点感到困惑,看起来很多人都是通过配置Nginx将来自特定域名的请求传递给FastCGI,然后由Go Web应用程序监听。类似于以下内容:

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

我不明白的是:在这里,我们有Nginx监听特定域名和特定IP,然后将其传递给fastcgi,然后再传递给Go。

为什么要这样做?我不能让Nginx 监听这个域名,然后让Go只监听这个域名吗?这样可以更高效地实现相同的效果,而且不需要反向代理。

由于我可以为Go监听的域名使用专用IP,所以(在我看来)不会有冲突。Nginx只需不监听123.123.123.123:80,而Go将监听123.123.123.123:80...因此不需要反向代理和fastcgi。

我的想法正确吗,还是我漏掉了什么?

如果是正确的,我只需要从Nginx中删除对该IP/域名的监听器,然后使用以下Go代码即可吗?

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

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

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. Something like this:

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.

Why do this? Can I not just have Nginx not listen for this domain, and then Go listen for this domain only? 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. 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.

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?

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

答案1

得分: 2

是的,你可以让Go直接在该IP上监听,它会正常工作。Nginx还有其他用途,比如缓存和提供静态文件。

此外,你可以让Go监听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/;
        }
}

需要进行几分钟的测试。

所以你要问自己,你是否需要Nginx来缓存一些文件或为你提供静态文件?如果是的,就在Go之前使用它;如果不需要,就直接使用Go。

此外,如果你要使用Nginx,代理方法可能比fcgi更快。

英文:

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.

Also you could have go listen on http.ListenAndServe("127.0.0.1:9020", nil) and proxy from nginx to it:

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? if yes, use it in front of go, if not, use go directly.

Also if you were to use nginx, the proxy method is probably faster than fcgi.

huangapple
  • 本文由 发表于 2014年7月26日 11:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/24967163.html
匿名

发表评论

匿名网友

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

确定