部署Go Web服务器到Google计算引擎

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

Deploy Go webserver to Google compute engine

问题

我刚开始测试Google计算引擎。现在我正在尝试在上面部署我的Go(golang)应用程序,以便可以从外部访问。我选择使用计算引擎而不是应用引擎,因为我的应用程序需要一个MongoDB数据库。

我按照以下步骤进行操作:

  1. 创建计算引擎实例。
  2. 设置防火墙,打开端口1234并设置静态IP。
  3. 安装MongoDB。
  4. 上传我的应用程序。
  5. 启动应用程序。

应用程序启动得很好。但是,如果我在浏览器中使用IP:1234打开它,我无法从外部访问它。我还尝试以root用户的身份在端口80上启动它,但也没有成功。

服务器的配置如下:

{
    "host": "localhost:1234",
    "dbhost": "localhost",
    "db": "dbname",
    "logfile": "log"
}

当我使用Apache服务器时,它可以提供端口80并显示页面...操作系统是Ubuntu 14.04。

主要问题可能出在以下代码中,它向mux添加了一些处理程序,并向公共目录添加了一个FileServer:

mux.Handle("/", http.FileServer(http.Dir(public_dir)))
// [...]
if err := http.ListenAndServe(cfg.Host, mux); err != nil {
    panic(err)
}

所以问题出在哪里呢?

英文:

I just started to test Google compute engine. Now I'm trying to deploy my Go (golang) application on it, so that it can be reached from outside. I use compute engine in favor of the app engine, since my application requires a MongoDB database.

I did the following:

  1. create compute engine instance
  2. setup up firewall so that port 1234 is open and IP is static
  3. install MongoDB
  4. upload my application
  5. start

The application starts just fine. But I cannot reach it from outside if I open it in my browser with ip:1234. I also tried to start it on port 80 as root user, but this didn't work neither.

The server is configured as following:

{
    "host": "localhost:1234",
    "dbhost": "localhost",
    "db": "dbname",
    "logfile": "log"
}

When I'm using an apache server it servers port 80 and the page is displayed... OS is ubuntu 14.04.

The main simply adds some handlers to a mux and adds a FileServer to the public dir:

mux.Handle("/", http.FileServer(http.Dir(public_dir)))
// [...]
if err := http.ListenAndServe(cfg.Host, mux); err != nil {
	panic(err)
}

So what's the issue here?

答案1

得分: 6

尝试将hostlocalhost更改为0.0.0.0,因为当前它只监听“内部”请求。

英文:

Try changing host from localhost to 0.0.0.0, because right now it's only listening to "inside" requests.

huangapple
  • 本文由 发表于 2016年1月4日 03:48:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/34580905.html
匿名

发表评论

匿名网友

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

确定