Skip to content

Commit 2c83dac

Browse files
zeripathsapk
authored andcommitted
FCGI: Allow FCGI over unix sockets (#9298)
* FCGI: Allow FCGI over unix sockets * fixup! FCGI: Allow FCGI over unix sockets
1 parent 4dc3993 commit 2c83dac

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

cmd/web.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func runWeb(ctx *cli.Context) error {
122122
switch setting.Protocol {
123123
case setting.UnixSocket:
124124
case setting.FCGI:
125+
case setting.FCGIUnix:
125126
default:
126127
// Save LOCAL_ROOT_URL if port changed
127128
cfg := ini.Empty()
@@ -149,7 +150,7 @@ func runWeb(ctx *cli.Context) error {
149150
}
150151

151152
listenAddr := setting.HTTPAddr
152-
if setting.Protocol != setting.UnixSocket {
153+
if setting.Protocol != setting.UnixSocket && setting.Protocol != setting.FCGIUnix {
153154
listenAddr += ":" + setting.HTTPPort
154155
}
155156
log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
@@ -183,10 +184,13 @@ func runWeb(ctx *cli.Context) error {
183184
err = runHTTPS("tcp", listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
184185
case setting.FCGI:
185186
NoHTTPRedirector()
186-
err = runFCGI(listenAddr, context2.ClearHandler(m))
187+
err = runFCGI("tcp", listenAddr, context2.ClearHandler(m))
187188
case setting.UnixSocket:
188189
NoHTTPRedirector()
189190
err = runHTTP("unix", listenAddr, context2.ClearHandler(m))
191+
case setting.FCGIUnix:
192+
NoHTTPRedirector()
193+
err = runFCGI("unix", listenAddr, context2.ClearHandler(m))
190194
default:
191195
log.Fatal("Invalid protocol: %s", setting.Protocol)
192196
}

cmd/web_graceful.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func NoMainListener() {
3737
graceful.Manager.InformCleanup()
3838
}
3939

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

4444
err := fcgiServer.ListenAndServe(func(listener net.Listener) error {
4545
return fcgi.Serve(listener, m)

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
140140

141141
## Server (`server`)
142142

143-
- `PROTOCOL`: **http**: \[http, https, fcgi, unix\]
143+
- `PROTOCOL`: **http**: \[http, https, fcgi, unix, fcgi+unix\]
144144
- `DOMAIN`: **localhost**: Domain name of this server.
145145
- `ROOT_URL`: **%(PROTOCOL)s://%(DOMAIN)s:%(HTTP\_PORT)s/**:
146146
Overwrite the automatically generated public URL.
@@ -155,7 +155,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
155155
- `HTTP_ADDR`: **0.0.0.0**: HTTP listen address.
156156
- If `PROTOCOL` is set to `fcgi`, Gitea will listen for FastCGI requests on TCP socket
157157
defined by `HTTP_ADDR` and `HTTP_PORT` configuration settings.
158-
- If `PROTOCOL` is set to `unix`, this should be the name of the Unix socket file to use.
158+
- If `PROTOCOL` is set to `unix` or `fcgi+unix`, this should be the name of the Unix socket file to use.
159159
- `HTTP_PORT`: **3000**: HTTP listen port.
160160
- If `PROTOCOL` is set to `fcgi`, Gitea will listen for FastCGI requests on TCP socket
161161
defined by `HTTP_ADDR` and `HTTP_PORT` configuration settings.

modules/setting/setting.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
HTTP Scheme = "http"
4343
HTTPS Scheme = "https"
4444
FCGI Scheme = "fcgi"
45+
FCGIUnix Scheme = "fcgi+unix"
4546
UnixSocket Scheme = "unix"
4647
)
4748

@@ -553,6 +554,14 @@ func NewContext() {
553554
KeyFile = sec.Key("KEY_FILE").String()
554555
case "fcgi":
555556
Protocol = FCGI
557+
case "fcgi+unix":
558+
Protocol = FCGIUnix
559+
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
560+
UnixSocketPermissionParsed, err := strconv.ParseUint(UnixSocketPermissionRaw, 8, 32)
561+
if err != nil || UnixSocketPermissionParsed > 0777 {
562+
log.Fatal("Failed to parse unixSocketPermission: %s", UnixSocketPermissionRaw)
563+
}
564+
UnixSocketPermission = uint32(UnixSocketPermissionParsed)
556565
case "unix":
557566
Protocol = UnixSocket
558567
UnixSocketPermissionRaw := sec.Key("UNIX_SOCKET_PERMISSION").MustString("666")
@@ -607,6 +616,8 @@ func NewContext() {
607616
defaultLocalURL = "http://unix/"
608617
case FCGI:
609618
defaultLocalURL = AppURL
619+
case FCGIUnix:
620+
defaultLocalURL = AppURL
610621
default:
611622
defaultLocalURL = string(Protocol) + "://"
612623
if HTTPAddr == "0.0.0.0" {

routers/routes/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func NewMacaron() *macaron.Macaron {
133133
if setting.EnableGzip {
134134
m.Use(gzip.Middleware())
135135
}
136-
if setting.Protocol == setting.FCGI {
136+
if setting.Protocol == setting.FCGI || setting.Protocol == setting.FCGIUnix {
137137
m.SetURLPrefix(setting.AppSubURL)
138138
}
139139
m.Use(public.Custom(

0 commit comments

Comments
 (0)