go之goapp systemd nginx之间的上游超时

yjmyzz 阅读:70 2025-06-02 22:19:02 评论:0

在 ubuntu 18.04LTS 上,我使用 nginx 反向代理我的 go 应用程序。

当我在命令行上运行 goapp 时它可以工作,但是当我试图将 goapp 置于 systemd 控制下时。 nginx 给出 "502 Bad Gateway".我认为这与 systemd、端口和 fork 有关?

任何帮助将不胜感激。 nginx 错误日志为每次连接尝试显示以下内容:

 2019/10/26 15:24:52 [error] 11974#11974: *1 upstream prematurely closed connection while reading response header from upstream, client: 217.42.177.154, server: _, request: "GET /codcall HTTP/1.1", upstream: "http://127.0.0.1:8001/codcall", host: "167.99.192.223" 

我的 systemd 应用程序配置如下:
[Unit] 
Description=CodCall web service 
 
[Service] 
Type=simple 
Restart=always 
RestartSec=5s 
ExecStart=/home/codcall/surf-get/codcall 
 
[Install] 
WantedBy=multi-user.target 

在 systemd 下运行时,我得到以下状态
codcall.service - CodCall web service 
   Loaded: loaded (/lib/systemd/system/codcall.service; disabled; vendor preset: enabled) 
   Active: active (running) since Sat 2019-10-26 15:24:07 UTC; 19min ago 
 Main PID: 11847 (codcall) 
    Tasks: 6 (limit: 1152) 
   CGroup: /system.slice/codcall.service 
           └─11847 /home/codcall/surf-get/codcall 
 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]: net/http.HandlerFunc.ServeHTTP(0x832a60, 0x8a2320, 0xc00025e0 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]:         /usr/lib/go-1.13/src/net/http/server.go:2007 +0x44 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]: net/http.(*ServeMux).ServeHTTP(0xb16240, 0x8a2320, 0xc00025e0 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]:         /usr/lib/go-1.13/src/net/http/server.go:2387 +0x1bd 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]: net/http.serverHandler.ServeHTTP(0xc0000ac000, 0x8a2320, 0xc0 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]:         /usr/lib/go-1.13/src/net/http/server.go:2802 +0xa4 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]: net/http.(*conn).serve(0xc0000783c0, 0x8a2c60, 0xc000014040) 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]:         /usr/lib/go-1.13/src/net/http/server.go:1890 +0x875 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]: created by net/http.(*Server).Serve 
Oct 26 15:37:41 codcall-ubuntu codcall[11847]:         /usr/lib/go-1.13/src/net/http/server.go:2927 +0x38e 

我的 Go 应用程序正在使用以下内容来收听和服务:
func main() { 
        http.HandleFunc("/", indexHandler) 
        http.ListenAndServe(":8001", nil) 
 
} 

我的 nginx 配置如下:我将/留在里面以检查 nginx 是否正在运行,并且仍然将/用作“欢迎使用 nginx”
server { 
        listen 80 default_server; 
        listen [::]:80 default_server; 
 
 location /codcall { 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_pass http://localhost:8001; 
   } 
 
 
        root /var/www/html; 
 
        # Add index.php to the list if you are using PHP 
        index index.html index.htm index.nginx-debian.html; 
 
        server_name _; 
 
        location / { 
                # First attempt to serve request as file, then 
                # as directory, then fall back to displaying a 404. 
                try_files $uri $uri/ =404; 
        } 
} 

请您参考如下方法:

原来在 systemd 下运行的 go 程序找不到我的模板文件。一旦我在 go 应用程序中输入模板文件的完整路径,它就可以工作了。

所以

t, _ := template.ParseFiles("template.gohtml") 

不得不成为
t, _ := template.ParseFiles("/home/myapp/mytemplates/template.gohtml") 

我还应该检查我的模板 var t 是否为 nil。


标签:nginx
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号