dongwen1909 2015-08-26 07:07 采纳率: 0%
浏览 99
已采纳

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

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 2015-08-26 08:08
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。