Skip to content

Commit

Permalink
FCGI: Allow FCGI over unix sockets (#9298)
Browse files Browse the repository at this point in the history
* FCGI: Allow FCGI over unix sockets

* fixup! FCGI: Allow FCGI over unix sockets
  • Loading branch information
zeripath authored and sapk committed Dec 10, 2019
1 parent 4dc3993 commit 2c83dac
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func runWeb(ctx *cli.Context) error {
switch setting.Protocol {
case setting.UnixSocket:
case setting.FCGI:
case setting.FCGIUnix:
default:
// Save LOCAL_ROOT_URL if port changed
cfg := ini.Empty()
Expand Down Expand Up @@ -149,7 +150,7 @@ func runWeb(ctx *cli.Context) error {
}

listenAddr := setting.HTTPAddr
if setting.Protocol != setting.UnixSocket {
if setting.Protocol != setting.UnixSocket && setting.Protocol != setting.FCGIUnix {
listenAddr += ":" + setting.HTTPPort
}
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
Expand Down Expand Up @@ -183,10 +184,13 @@ func runWeb(ctx *cli.Context) error {
err = runHTTPS("tcp", listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
case setting.FCGI:
NoHTTPRedirector()
err = runFCGI(listenAddr, context2.ClearHandler(m))
err = runFCGI("tcp", listenAddr, context2.ClearHandler(m))
case setting.UnixSocket:
NoHTTPRedirector()
err = runHTTP("unix", listenAddr, context2.ClearHandler(m))
case setting.FCGIUnix:
NoHTTPRedirector()
err = runFCGI("unix", listenAddr, context2.ClearHandler(m))
default:
log.Fatal("Invalid protocol: %s", setting.Protocol)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/web_graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func NoMainListener() {
graceful.Manager.InformCleanup()
}

func runFCGI(listenAddr string, m http.Handler) error {
func runFCGI(network, listenAddr string, m http.Handler) error {
// This needs to handle stdin as fcgi point
fcgiServer := graceful.NewServer("tcp", listenAddr)
fcgiServer := graceful.NewServer(network, listenAddr)

err := fcgiServer.ListenAndServe(func(listener net.Listener) error {
return fcgi.Serve(listener, m)
Expand Down
4 changes: 2 additions & 2 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.

## Server (`server`)

- `PROTOCOL`: **http**: \[http, https, fcgi, unix\]
- `PROTOCOL`: **http**: \[http, https, fcgi, unix, fcgi+unix\]
- `DOMAIN`: **localhost**: Domain name of this server.
- `ROOT_URL`: **%(PROTOCOL)s://%(DOMAIN)s:%(HTTP\_PORT)s/**:
Overwrite the automatically generated public URL.
Expand All @@ -155,7 +155,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `HTTP_ADDR`: **0.0.0.0**: HTTP listen address.
- If `PROTOCOL` is set to `fcgi`, Gitea will listen for FastCGI requests on TCP socket
defined by `HTTP_ADDR` and `HTTP_PORT` configuration settings.
- If `PROTOCOL` is set to `unix`, this should be the name of the Unix socket file to use.
- If `PROTOCOL` is set to `unix` or `fcgi+unix`, this should be the name of the Unix socket file to use.
- `HTTP_PORT`: **3000**: HTTP listen port.
- If `PROTOCOL` is set to `fcgi`, Gitea will listen for FastCGI requests on TCP socket
defined by `HTTP_ADDR` and `HTTP_PORT` configuration settings.
Expand Down
11 changes: 11 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
HTTP Scheme = "http"
HTTPS Scheme = "https"
FCGI Scheme = "fcgi"
FCGIUnix Scheme = "fcgi+unix"
UnixSocket Scheme = "unix"
)

Expand Down Expand Up @@ -553,6 +554,14 @@ func NewContext() {
KeyFile = sec.Key("KEY_FILE").String()
case "fcgi":
Protocol = FCGI
case "fcgi+unix":
Protocol = FCGIUnix
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
UnixSocketPermissionParsed, err := strconv.ParseUint(UnixSocketPermissionRaw, 8, 32)
if err != nil || UnixSocketPermissionParsed > 0777 {
log.Fatal("Failed to parse unixSocketPermission: %s", UnixSocketPermissionRaw)
}
UnixSocketPermission = uint32(UnixSocketPermissionParsed)
case "unix":
Protocol = UnixSocket
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
Expand Down Expand Up @@ -607,6 +616,8 @@ func NewContext() {
defaultLocalURL = "http://unix/"
case FCGI:
defaultLocalURL = AppURL
case FCGIUnix:
defaultLocalURL = AppURL
default:
defaultLocalURL = string(Protocol) + "://"
if HTTPAddr == "0.0.0.0" {
Expand Down
2 changes: 1 addition & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func NewMacaron() *macaron.Macaron {
if setting.EnableGzip {
m.Use(gzip.Middleware())
}
if setting.Protocol == setting.FCGI {
if setting.Protocol == setting.FCGI || setting.Protocol == setting.FCGIUnix {
m.SetURLPrefix(setting.AppSubURL)
}
m.Use(public.Custom(
Expand Down

0 comments on commit 2c83dac

Please sign in to comment.