I
I
i-zemlya2019-04-04 14:06:33
Nginx
i-zemlya, 2019-04-04 14:06:33

Nginx, and a simple Golang application?

I'm trying to write a simple application on GO

package main

import (
    "net"
    "net/http"
    "net/http/fcgi"
    "os"
)

func main() {
    s := "/tmp/go.sock"
    os.Remove(s)
    l, err := net.Listen("unix", s)
    if err != nil {
        os.Exit(2)
    }
    fs := new(FCgiServer)
    fcgi.Serve(l, fs)
}

type (
    FCgiServer struct {}
)

func (this *FCgiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    defer r.Body.Close()
    defer func() {
        if err := recover(); err != nil {
            w.WriteHeader(500)
            w.Write([]byte(`500 Server error`))
        }
    }()
    w.Write([]byte(`OK Server<br>text`))
}

config for nginx:
server {
    listen 80;
    root /home/username/go/src/github.com/i-zemlya/cli/public;
    location ~ {
        try_files $uri = 404;
        fastcgi_pass unix:/tmp/go.sock;
        include fastcgi_params;
    }
}

I get errors in nginx logs:
2019/04/04 12:00:00 [error] 444#444: *1 rewrite or internal redirection cycle while internally redirecting to "404", client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", host: "127.0.0.1:80"
2019/04/04 12:00:00 [error] 444#444: *2 rewrite or internal redirection cycle while internally redirecting to "404", client: 127.0.0.1, server: 127.0.0.1, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:80", referrer: "http://127.0.0.1:80/"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-04-04
@i-zemlya

First, why so strange location? Should be location /.
Second, try_filesit's not needed.
As a result, it turns out something like this:

server {
    listen 80;
    root /home/username/go/src/github.com/i-zemlya/cli/public;
    location / {
        fastcgi_pass unix:/tmp/go.sock;
        include fastcgi_params;
    }
    location /static/ {
        # раздаём картинки из $root/static/
    }
}

We put all pictures, scripts, styles and other statics in a folder
/home/username/go/src/github.com/i-zemlya/cli/public/static/
and they will be available at /static/.....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question