dongwen1909
dongwen1909
2015-08-26 07:07

托管在AWS上时无法访问Go服务器

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

I have created a project in Go using gin and it is working fine locally. However, when I tried deploying this on an EC2 instance on AWS, I was unable to access the APIs on the server.

I did a ssh into the hosted machine and gave a curl request (curl localhost:8080) and it gave a proper response. But any request from outside is not reachable.

The server is running on port 8080. I have opened the ports in the AWS security groups.

Is there any setting in Go/gin that I need to make for it to be accessible from the internet?

Sample code:

package main

import (
    "myConstants"
    "myDatabase"
    "myMiddleware"
    "onboarding"

    "github.com/gin-gonic/gin"
)

func main() {

    var db = myDatabase.DBConnect()

    router := gin.Default()

    router.Use(myMiddleware.RestrictInputContent)
    router.Use(myMiddleware.CheckToken(db))
    router.Use(myMiddleware.RequestLoggerMiddleware())

    router.POST("/signup", onboarding.Signup(db))
    router.POST("/login", onboarding.Login(db))
    router.POST("/logout", onboarding.Logout(db))
    router.GET("/", onboarding.Hello(db))

    defer db.Close()

    //Listen and serve
    router.Run("127.0.0.1:8080")

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • du4822 du4822 6年前

    Changed the router.Run from router.Run("127.0.0.1:8080") to router.Run(":8080") and it works. As suggested by @elithrar and @user3591723 127.0.0.1 (local host) is only the loop back interface on the machine Binding to ":8080" means 0.0.0.0:8080 - which means all interfaces

    点赞 评论 复制链接分享

为你推荐