From 74b28cbf2349e7dc859dadfad90426d688aed931 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Thu, 24 Nov 2022 16:06:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0nginx=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../versions/1.23.1/conf/conf.d/default.conf | 9 +++- backend/app/api/v1/nginx.go | 10 +++++ backend/app/dto/nginx.go | 10 +++++ backend/app/service/nginx.go | 25 +++++++++++ backend/router/ro_nginx.go | 1 + frontend/src/api/interface/nginx.ts | 10 +++++ frontend/src/api/modules/nginx.ts | 4 ++ frontend/src/lang/modules/zh.ts | 8 ++++ .../src/views/website/website/nginx/index.vue | 4 ++ .../website/website/nginx/status/index.vue | 42 +++++++++++++++++++ 10 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 frontend/src/views/website/website/nginx/status/index.vue diff --git a/apps/nginx/versions/1.23.1/conf/conf.d/default.conf b/apps/nginx/versions/1.23.1/conf/conf.d/default.conf index bb0d1f3a2..da6594f8f 100644 --- a/apps/nginx/versions/1.23.1/conf/conf.d/default.conf +++ b/apps/nginx/versions/1.23.1/conf/conf.d/default.conf @@ -1,6 +1,6 @@ server { - listen 80 default_server; - server_name _; + listen 80 ; + server_name 127.0.0.1; charset utf-8; default_type text/html; @@ -9,5 +9,10 @@ server { root /usr/share/nginx/html; } + location /nginx_status { + stub_status on; + access_log off; + } + root /usr/share/nginx/html; } \ No newline at end of file diff --git a/backend/app/api/v1/nginx.go b/backend/app/api/v1/nginx.go index c919e5abe..ba81e78e5 100644 --- a/backend/app/api/v1/nginx.go +++ b/backend/app/api/v1/nginx.go @@ -47,3 +47,13 @@ func (b *BaseApi) UpdateNginxConfigBy(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) GetNginxStatus(c *gin.Context) { + + res, err := nginxService.GetStatus() + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, res) +} diff --git a/backend/app/dto/nginx.go b/backend/app/dto/nginx.go index 12e64aee9..15fa40a7b 100644 --- a/backend/app/dto/nginx.go +++ b/backend/app/dto/nginx.go @@ -20,6 +20,16 @@ type NginxScopeReq struct { Scope NginxKey `json:"scope"` } +type NginxStatus struct { + Active string `json:"active"` + Accepts string `json:"accepts"` + Handled string `json:"handled"` + Requests string `json:"requests"` + Reading string `json:"reading"` + Writing string `json:"writing"` + Waiting string `json:"waiting"` +} + type NginxKey string const ( diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index 109235d3d..16797d5ab 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -1,10 +1,13 @@ package service import ( + "fmt" "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/files" "path" + "strings" ) type NginxService struct { @@ -46,3 +49,25 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error { } return updateHttpNginxConfig(getNginxParams(req.Params, keys)) } + +func (n NginxService) GetStatus() (dto.NginxStatus, error) { + nginxInstall, err := getAppInstallByKey("nginx") + if err != nil { + return dto.NginxStatus{}, err + } + res, err := cmd.Exec(fmt.Sprintf("docker exec -i %s curl http://127.0.0.1/nginx_status", nginxInstall.ContainerName)) + if err != nil { + return dto.NginxStatus{}, err + } + var status dto.NginxStatus + resArray := strings.Split(res, " ") + status.Active = resArray[2] + status.Accepts = resArray[7] + status.Handled = resArray[8] + status.Requests = resArray[9] + status.Reading = resArray[11] + status.Writing = resArray[13] + status.Waiting = resArray[15] + + return status, nil +} diff --git a/backend/router/ro_nginx.go b/backend/router/ro_nginx.go index 67f151c67..eb9632f35 100644 --- a/backend/router/ro_nginx.go +++ b/backend/router/ro_nginx.go @@ -18,5 +18,6 @@ func (a *NginxRouter) InitNginxRouter(Router *gin.RouterGroup) { groupRouter.GET("", baseApi.GetNginx) groupRouter.POST("/scope", baseApi.GetNginxConfigByScope) groupRouter.POST("/update", baseApi.UpdateNginxConfigBy) + groupRouter.GET("/status", baseApi.GetNginxStatus) } } diff --git a/frontend/src/api/interface/nginx.ts b/frontend/src/api/interface/nginx.ts index c398c144d..409161f1c 100644 --- a/frontend/src/api/interface/nginx.ts +++ b/frontend/src/api/interface/nginx.ts @@ -14,4 +14,14 @@ export namespace Nginx { scope: string; params?: any; } + + export interface NginxStatus { + accepts: string; + handled: string; + active: string; + requests: string; + reading: string; + writing: string; + waiting: string; + } } diff --git a/frontend/src/api/modules/nginx.ts b/frontend/src/api/modules/nginx.ts index 3f734fd30..f007c8985 100644 --- a/frontend/src/api/modules/nginx.ts +++ b/frontend/src/api/modules/nginx.ts @@ -13,3 +13,7 @@ export const GetNginxConfigByScope = (req: Nginx.NginxScopeReq) => { export const UpdateNginxConfigByScope = (req: Nginx.NginxConfigReq) => { return http.post(`/nginx/update`, req); }; + +export const GetNginxStatus = () => { + return http.get(`/nginx/status`); +}; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index ea922a7b3..cd60c76f1 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -769,5 +769,13 @@ export default { gzipMinLengthHelper: '最小压缩文件', gzipCompLevelHelper: '压缩率', gzipHelper: '是否开启压缩传输', + connections: '活动连接(Active connections)', + accepts: '总连接次数(accepts)', + handled: '总握手次数(handled)', + requests: '总握手次数(requests)', + reading: '请求数(Reading)', + writing: '响应数(Writing)', + waiting: '驻留进程(Waiting)', + status: '负载状态', }, }; diff --git a/frontend/src/views/website/website/nginx/index.vue b/frontend/src/views/website/website/nginx/index.vue index 8b5fddc66..55ce91b51 100644 --- a/frontend/src/views/website/website/nginx/index.vue +++ b/frontend/src/views/website/website/nginx/index.vue @@ -10,6 +10,9 @@ + + + @@ -20,6 +23,7 @@ import Source from './source/index.vue'; import { ref, watch } from 'vue'; import ContainerLog from '@/components/container-log/index.vue'; import NginxPer from './performance/index.vue'; +import Status from './status/index.vue'; let activeName = ref('1'); let dialogContainerLogRef = ref(); diff --git a/frontend/src/views/website/website/nginx/status/index.vue b/frontend/src/views/website/website/nginx/status/index.vue new file mode 100644 index 000000000..2ff76eeec --- /dev/null +++ b/frontend/src/views/website/website/nginx/status/index.vue @@ -0,0 +1,42 @@ + + +