Skip to content

Commit d70e37c

Browse files
committed
fix: taking 100% CPU if the log file is not a regular file
1 parent f20d97a commit d70e37c

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

api/nginx/nginx_log.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package nginx
33
import (
44
"encoding/json"
55
"github.com/0xJacky/Nginx-UI/api"
6-
"github.com/0xJacky/Nginx-UI/internal/helper"
76
"github.com/0xJacky/Nginx-UI/internal/logger"
87
"github.com/0xJacky/Nginx-UI/internal/nginx"
98
"github.com/gin-gonic/gin"
@@ -50,15 +49,21 @@ func GetNginxLogPage(c *gin.Context) {
5049
return
5150
}
5251

53-
f, err := os.Open(logPath)
52+
logFileStat, err := os.Stat(logPath)
5453

5554
if err != nil {
5655
c.JSON(http.StatusOK, nginxLogPageResp{})
5756
logger.Error(err)
5857
return
5958
}
6059

61-
logFileStat, err := os.Stat(logPath)
60+
if !logFileStat.Mode().IsRegular() {
61+
c.JSON(http.StatusOK, nginxLogPageResp{})
62+
logger.Error("log file is not regular file:", logPath)
63+
return
64+
}
65+
66+
f, err := os.Open(logPath)
6267

6368
if err != nil {
6469
c.JSON(http.StatusOK, nginxLogPageResp{})
@@ -188,8 +193,16 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
188193
Whence: io.SeekEnd,
189194
}
190195

191-
if !helper.FileExists(logPath) {
192-
errChan <- errors.New("error log path not exists " + logPath)
196+
stat, err := os.Stat(logPath)
197+
if os.IsNotExist(err) {
198+
errChan <- errors.New("[error] log path not exists " + logPath)
199+
return
200+
}
201+
202+
if !stat.Mode().IsRegular() {
203+
errChan <- errors.New("[error] " + logPath + " is not a regular file. " +
204+
"If you are using nginx-ui in docker container, please refer to " +
205+
"https://nginxui.com/zh_CN/guide/config-nginx-log.html for more information.")
193206
return
194207
}
195208

app/src/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"2.0.0-beta.11","build_id":109,"total_build":313}
1+
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}

app/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"2.0.0-beta.11","build_id":109,"total_build":313}
1+
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}

resources/demo/app.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ StartCmd = bash
88
NodeSecret = fdc7764f-92d2-454c-9640-6a09be121139
99
Demo = true
1010

11-
[nginx_log]
11+
[nginx]
1212
AccessLogPath = /var/log/nginx/access.local.log
1313
ErrorLogPath = /var/log/nginx/error.local.log
1414

0 commit comments

Comments
 (0)