goapp systemd nginx之间的上游超时

问题描述 投票:0回答:1

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

当我在命令行上运行goapp时,但是当我试图将goapp置于systemd控制之下时,它会起作用。 nginx给出了一个"502 Bad Gateway".,我认为这与systemd,ports以及分叉有关?

任何帮助将不胜感激。对于每次连接尝试,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;
        }
}
go nginx reverse-proxy ubuntu-18.04 systemd
1个回答
0
投票

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

所以

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

必须成为

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

我也应该检查模板变量是否为零。

© www.soinside.com 2019 - 2024. All rights reserved.