Skip to content

Instantly share code, notes, and snippets.

@atomaths
Last active April 17, 2024 16:11
Show Gist options
  • Save atomaths/5403041 to your computer and use it in GitHub Desktop.
Save atomaths/5403041 to your computer and use it in GitHub Desktop.
This is a FastCGI example server in Go.
## FastCGI
server {
location ~ /app.* {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
}
## Reverse Proxy (이 방식으로 하면 http.ListenAndServe로 해야함)
server {
listen 80;
# listen 443 ssl;
server_name api.golang.org;
location ~ / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:3000;
}
}
package main
import (
"fmt"
"net/http"
"net/http/fcgi"
"net"
)
type FastCGIServer struct{}
func (s FastCGIServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("This is a FastCGI example server.\n"))
}
func main() {
fmt.Println("Starting server...")
l, _ := net.Listen("tcp", "127.0.0.1:9000")
b := new(FastCGIServer)
fcgi.Serve(l, b)
}
// 아래처럼 HandleFunc를 등록해서도 처리가능
func main() {
http.HandleFunc("/foo", fooHandler)
l, err := net.Listen("tcp", "127.0.0.1:9000")
if err != nil {
panic(err)
}
fcgi.Serve(l, nil)
}
func fooHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello")
}
@patrickgh3
Copy link

Thank you!

@GwynethLlewelyn
Copy link

Can you set up several http.HandleFunc() with fcgi.Serve()? The documentation says it's possible, but I can't get it to work, even though http.DefaultServeMux shows the correct configuration...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment