英文:
Deploy Go webserver to Google compute engine
问题
我刚开始测试Google计算引擎。现在我正在尝试在上面部署我的Go(golang)应用程序,以便可以从外部访问。我选择使用计算引擎而不是应用引擎,因为我的应用程序需要一个MongoDB数据库。
我按照以下步骤进行操作:
- 创建计算引擎实例。
- 设置防火墙,打开端口1234并设置静态IP。
- 安装MongoDB。
- 上传我的应用程序。
- 启动应用程序。
应用程序启动得很好。但是,如果我在浏览器中使用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:
- create compute engine instance
- setup up firewall so that port 1234 is open and IP is static
- install MongoDB
- upload my application
- 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
尝试将host
从localhost
更改为0.0.0.0
,因为当前它只监听“内部”请求。
英文:
Try changing host
from localhost
to 0.0.0.0
, because right now it's only listening to "inside" requests.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论