Skip to content

Commit 8b565a4

Browse files
authored
Refactor nginx path resolution with improved regex and fallback
Updated regex patterns for extracting nginx configuration paths and added fallback mechanisms for determining paths on different operating systems. Improved error handling and logging for better debugging.
1 parent b26f40d commit 8b565a4

1 file changed

Lines changed: 92 additions & 51 deletions

File tree

internal/nginx/resolve_path.go

Lines changed: 92 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nginx
22

33
import (
4+
"os"
45
"path/filepath"
56
"regexp"
67
"runtime"
@@ -57,64 +58,104 @@ func GetPrefix() string {
5758
return nginxPrefix
5859
}
5960

60-
// GetConfPath returns the path of the nginx configuration file
61+
// GetConfPath returns the nginx configuration directory (e.g. "/etc/nginx").
62+
// It tries to derive it from `nginx -V --conf-path=...`.
63+
// If parsing fails, it falls back to a reasonable default instead of returning "".
6164
func GetConfPath(dir ...string) (confPath string) {
62-
if settings.NginxSettings.ConfigDir == "" {
63-
out := getNginxV()
64-
r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
65-
match := r.FindStringSubmatch(out)
66-
if len(match) < 1 {
67-
logger.Error("nginx.GetConfPath len(match) < 1")
68-
return ""
69-
}
70-
confPath = match[1]
71-
} else {
72-
confPath = settings.NginxSettings.ConfigDir
73-
}
74-
75-
confPath = resolvePath(confPath)
76-
77-
joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
78-
if !helper.IsUnderDirectory(joined, confPath) {
79-
return confPath
80-
}
81-
return joined
65+
if settings.NginxSettings.ConfigDir == "" {
66+
out := getNginxV()
67+
r, _ := regexp.Compile(`--conf-path=([^\s]+)`)
68+
match := r.FindStringSubmatch(out)
69+
70+
if len(match) > 1 {
71+
fullConf := match[1]
72+
confPath = filepath.Dir(fullConf)
73+
} else {
74+
if runtime.GOOS == "windows" {
75+
confPath = GetPrefix()
76+
} else {
77+
confPath = "/etc/nginx"
78+
}
79+
80+
logger.Debug("nginx.GetConfPath fallback used", "base", confPath)
81+
}
82+
} else {
83+
confPath = settings.NginxSettings.ConfigDir
84+
}
85+
86+
confPath = resolvePath(confPath)
87+
88+
joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
89+
if !helper.IsUnderDirectory(joined, confPath) {
90+
return confPath
91+
}
92+
return joined
8293
}
8394

84-
// GetConfEntryPath returns the path of the nginx configuration file
95+
// GetConfEntryPath returns the absolute path to the main nginx.conf.
96+
// It prefers the value from `nginx -V --conf-path=...`.
97+
// If that can't be parsed, it falls back to "<confDir>/nginx.conf".
8598
func GetConfEntryPath() (path string) {
86-
if settings.NginxSettings.ConfigPath == "" {
87-
out := getNginxV()
88-
r, _ := regexp.Compile("--conf-path=(.*.conf)")
89-
match := r.FindStringSubmatch(out)
90-
if len(match) < 1 {
91-
logger.Error("nginx.GetConfEntryPath len(match) < 1")
92-
return ""
93-
}
94-
path = match[1]
95-
} else {
96-
path = settings.NginxSettings.ConfigPath
97-
}
98-
99-
return resolvePath(path)
99+
if settings.NginxSettings.ConfigPath == "" {
100+
out := getNginxV()
101+
r, _ := regexp.Compile(`--conf-path=([^\s]+)`)
102+
match := r.FindStringSubmatch(out)
103+
104+
if len(match) > 1 {
105+
path = match[1]
106+
} else {
107+
baseDir := GetConfPath()
108+
109+
if baseDir != "" {
110+
path = filepath.Join(baseDir, "nginx.conf")
111+
} else {
112+
logger.Error("nginx.GetConfEntryPath: cannot determine nginx.conf path")
113+
path = ""
114+
}
115+
}
116+
} else {
117+
path = settings.NginxSettings.ConfigPath
118+
}
119+
120+
return resolvePath(path)
100121
}
101122

102-
// GetPIDPath returns the path of the nginx PID file
123+
// GetPIDPath returns the nginx master process PID file path.
124+
// We try to read it from `nginx -V --pid-path=...`.
125+
// If that fails (which often happens in container images), we probe common
126+
// locations like /run/nginx.pid and /var/run/nginx.pid instead of just failing.
103127
func GetPIDPath() (path string) {
104-
if settings.NginxSettings.PIDPath == "" {
105-
out := getNginxV()
106-
r, _ := regexp.Compile("--pid-path=(.*.pid)")
107-
match := r.FindStringSubmatch(out)
108-
if len(match) < 1 {
109-
logger.Error("pid path not found in nginx -V output")
110-
return ""
111-
}
112-
path = match[1]
113-
} else {
114-
path = settings.NginxSettings.PIDPath
115-
}
116-
117-
return resolvePath(path)
128+
if settings.NginxSettings.PIDPath == "" {
129+
out := getNginxV()
130+
r, _ := regexp.Compile(`--pid-path=([^\s]+)`)
131+
match := r.FindStringSubmatch(out)
132+
133+
if len(match) > 1 {
134+
path = match[1]
135+
} else {
136+
candidates := []string{
137+
"/var/run/nginx.pid",
138+
"/run/nginx.pid",
139+
}
140+
141+
for _, c := range candidates {
142+
if _, err := os.Stat(c); err == nil {
143+
logger.Debug("GetPIDPath fallback hit", "path", c)
144+
path = c
145+
break
146+
}
147+
}
148+
149+
if path == "" {
150+
logger.Error("GetPIDPath: could not determine PID path")
151+
return ""
152+
}
153+
}
154+
} else {
155+
path = settings.NginxSettings.PIDPath
156+
}
157+
158+
return resolvePath(path)
118159
}
119160

120161
// GetSbinPath returns the path of the nginx executable

0 commit comments

Comments
 (0)