Skip to content

Commit 1fd24eb

Browse files
committed
feat(nginx): add SbinPath option in settings
1 parent ccebcfd commit 1fd24eb

File tree

10 files changed

+928
-84
lines changed

10 files changed

+928
-84
lines changed

app/src/constants/errors/nginx.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export default {
22
50000: () => $gettext('Nginx error: {0}'),
33
50001: () => $gettext('Block is nil'),
44
50002: () => $gettext('Reload nginx failed: {0}'),
5+
50003: () => $gettext('Nginx -T output is empty'),
56
}

docs/guide/config-nginx.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ In Nginx UI v2, we parse the output of the `nginx -V` command to get the default
6969

7070
If you need to override the default path, you can use this option.
7171

72+
### SbinPath
73+
- Type: `string`
74+
- Version: `>= v2.1.10`
75+
76+
This option is used to set the path for the Nginx executable file.
77+
78+
By default, Nginx UI will try to find the Nginx executable file in `$PATH`.
79+
80+
If you need to override the default path, you can use this option.
81+
7282
### TestConfigCmd
7383
- Type: `string`
7484
- Default: `nginx -t`

docs/zh_CN/guide/config-nginx.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ Nginx 日志对于监控、排查问题和维护您的 Web 服务器至关重要
7070

7171
如果您需要覆盖默认路径,您可以使用此选项。
7272

73+
### SbinPath
74+
- 类型:`string`
75+
- 版本:`>= v2.1.10`
76+
77+
此选项用于设置 Nginx 可执行文件的路径。
78+
79+
默认情况下,Nginx UI 会尝试在 `$PATH` 中查找 Nginx 可执行文件。
80+
81+
如果您需要覆盖默认路径,您可以使用此选项。
82+
7383
### TestConfigCmd
7484
- 类型:`string`
7585
- 默认值:`nginx -t`

docs/zh_TW/guide/config-nginx.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ Nginx 日誌對於監控、排查問題和維護您的 Web 伺服器至關重要
6969

7070
如果您需要覆蓋預設路徑,您可以使用此選項。
7171

72+
### SbinPath
73+
- 類型:`string`
74+
- 版本:`>= v2.1.10`
75+
76+
此選項用於設定 Nginx 可執行檔的路徑。
77+
78+
預設情況下,Nginx UI 會嘗試在 `$PATH` 中查找 Nginx 可執行檔。
79+
80+
如果您需要覆蓋預設路徑,您可以使用此選項。
81+
7282
### TestConfigCmd
7383
- 類型:`string`
7484
- 預設值:`nginx -t`
File renamed without changes.

internal/nginx/log_path.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package nginx
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"regexp"
7+
"strings"
8+
9+
"github.com/uozi-tech/cosy/logger"
10+
)
11+
12+
// Regular expressions for parsing log directives from nginx -T output
13+
const (
14+
// AccessLogRegexPattern matches access_log directive with unquoted path
15+
// Matches: access_log /path/to/file
16+
AccessLogRegexPattern = `(?m)^\s*access_log\s+([^\s;]+)`
17+
18+
// ErrorLogRegexPattern matches error_log directive with unquoted path
19+
// Matches: error_log /path/to/file
20+
ErrorLogRegexPattern = `(?m)^\s*error_log\s+([^\s;]+)`
21+
)
22+
23+
var (
24+
accessLogRegex *regexp.Regexp
25+
errorLogRegex *regexp.Regexp
26+
)
27+
28+
func init() {
29+
accessLogRegex = regexp.MustCompile(AccessLogRegexPattern)
30+
errorLogRegex = regexp.MustCompile(ErrorLogRegexPattern)
31+
}
32+
33+
// isValidRegularFile checks if the given path is a valid regular file
34+
// Returns true if the path exists and is a regular file (not a directory or special file)
35+
func isValidRegularFile(path string) bool {
36+
if path == "" {
37+
return false
38+
}
39+
40+
fileInfo, err := os.Stat(path)
41+
if err != nil {
42+
logger.Debug("nginx.isValidRegularFile: failed to stat file", "path", path, "error", err)
43+
return false
44+
}
45+
46+
// Check if it's a regular file (not a directory or special file)
47+
if !fileInfo.Mode().IsRegular() {
48+
logger.Debug("nginx.isValidRegularFile: path is not a regular file", "path", path, "mode", fileInfo.Mode())
49+
return false
50+
}
51+
52+
return true
53+
}
54+
55+
// isCommentedLine checks if a line is commented (starts with #)
56+
func isCommentedLine(line string) bool {
57+
trimmed := strings.TrimSpace(line)
58+
return strings.HasPrefix(trimmed, "#")
59+
}
60+
61+
// getAccessLogPathFromNginxT extracts the first access_log path from nginx -T output
62+
func getAccessLogPathFromNginxT() string {
63+
output := getNginxT()
64+
if output == "" {
65+
logger.Error("nginx.getAccessLogPathFromNginxT: nginx -T output is empty")
66+
return ""
67+
}
68+
69+
lines := strings.Split(output, "\n")
70+
71+
for _, line := range lines {
72+
// Skip commented lines
73+
if isCommentedLine(line) {
74+
continue
75+
}
76+
77+
matches := accessLogRegex.FindStringSubmatch(line)
78+
if len(matches) >= 2 {
79+
logPath := matches[1]
80+
81+
// Skip 'off' directive
82+
if logPath == "off" {
83+
continue
84+
}
85+
// Handle relative paths
86+
if !filepath.IsAbs(logPath) {
87+
logPath = filepath.Join(GetPrefix(), logPath)
88+
}
89+
resolvedPath := resolvePath(logPath)
90+
91+
// Validate that the path is a regular file
92+
if !isValidRegularFile(resolvedPath) {
93+
logger.Warn("nginx.getAccessLogPathFromNginxT: path is not a valid regular file", "path", resolvedPath)
94+
continue
95+
}
96+
97+
return resolvedPath
98+
}
99+
}
100+
101+
logger.Error("nginx.getAccessLogPathFromNginxT: no valid access_log file found")
102+
return ""
103+
}
104+
105+
// getErrorLogPathFromNginxT extracts the first error_log path from nginx -T output
106+
func getErrorLogPathFromNginxT() string {
107+
output := getNginxT()
108+
if output == "" {
109+
logger.Error("nginx.getErrorLogPathFromNginxT: nginx -T output is empty")
110+
return ""
111+
}
112+
113+
lines := strings.Split(output, "\n")
114+
115+
for _, line := range lines {
116+
// Skip commented lines
117+
if isCommentedLine(line) {
118+
continue
119+
}
120+
121+
matches := errorLogRegex.FindStringSubmatch(line)
122+
if len(matches) >= 2 {
123+
logPath := matches[1]
124+
125+
// Handle relative paths
126+
if !filepath.IsAbs(logPath) {
127+
logPath = filepath.Join(GetPrefix(), logPath)
128+
}
129+
resolvedPath := resolvePath(logPath)
130+
131+
// Validate that the path is a regular file
132+
if !isValidRegularFile(resolvedPath) {
133+
logger.Warn("nginx.getErrorLogPathFromNginxT: path is not a valid regular file", "path", resolvedPath)
134+
continue
135+
}
136+
137+
return resolvedPath
138+
}
139+
}
140+
141+
logger.Error("nginx.getErrorLogPathFromNginxT: no valid error_log file found")
142+
return ""
143+
}

0 commit comments

Comments
 (0)