فهرست منبع

deploy via docker #20

0xJacky 2 سال پیش
والد
کامیت
9851fe8fca

+ 1 - 1
.gitignore

@@ -7,4 +7,4 @@ app.ini
 dist
 *.exe
 *.po~
-nginx-ui-server
+nginx-ui

+ 12 - 10
Dockerfile

@@ -1,11 +1,13 @@
-# CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui-server -v main.go
-FROM --platform=linux/amd64 uozi/nginx-ui-demo-debian-base-slim:latest
+# CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
+FROM --platform=linux/amd64 uozi/nginx-ui-base:latest
 WORKDIR /app
-EXPOSE 80
-COPY ./resources/demo/nginx.conf /etc/nginx/sites-available/default
-COPY ./resources/demo/app.ini /app/app.ini
-COPY ./resources/demo/demo.db /app/database.db
-COPY ./resources/demo/start.sh /app/start.sh
-COPY ./nginx-ui-server /app/nginx-ui
-RUN cd /app && chmod a+x start.sh
-CMD ["./start.sh"]
+EXPOSE 80 443
+
+COPY resources/docker/start.sh /app/start.sh
+COPY resources/docker/nginx.conf /usr/etc/nginx/nginx.conf
+COPY resources/docker/nginx-ui.conf /usr/etc/nginx/conf.d/default.conf
+COPY ./nginx-ui /app/nginx-ui
+
+RUN cd /app && chmod a+x /app/start.sh
+
+ENTRYPOINT ["./start.sh"]

+ 21 - 0
README-zh_CN.md

@@ -35,6 +35,7 @@ Nginx 网络管理界面,由  [0xJacky](https://jackyu.cn/) 与 [Hintay](https
           <ul>
             <li><a href="#通过执行文件运行">通过执行文件运行</a></li>
             <li><a href="#使用-systemd">使用 Systemd</a></li>
+            <li><a href="#使用-Docker">使用 Docker</a></li>
           </ul>
         </li>
       </ul>
@@ -158,6 +159,26 @@ systemctl stop nginx-ui
 ```shell
 systemctl restart nginx-ui
 ```
+
+## 使用 Docker
+
+Docker 示例
+- `nginx-ui:latest` 镜像基于 `nginx:latest` 构建,
+您可以直接将该镜像监听到 80 和 443 端口以取代宿主机上的 Nginx
+
+- 映射到 `/etc/nginx` 的文件夹应该为一个空目录
+
+```
+docker run -dit \
+  --name=nginx-ui \
+  --restart=always \
+  -e TZ=Asia/Shanghai \
+  -v /mnt/user/appdata/nginx:/etc/nginx \
+  -v /mnt/user/appdata/nginx-ui:/etc/nginx-ui \
+  -p 8080:80 -p 8443:443 \
+  nginx-ui:latest
+```
+
 ## 手动构建
 
 对于没有官方构建版本的平台,可以尝试手动构建。

+ 8 - 0
build-demo.sh

@@ -0,0 +1,8 @@
+#!/bin/bash
+CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc \
+    CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags \
+    "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
+
+docker build -t nginx-ui-demo .
+docker tag nginx-ui-demo uozi/nginx-ui-demo
+docker push uozi/nginx-ui-demo

+ 7 - 4
build.sh

@@ -1,5 +1,8 @@
 #!/bin/bash
-CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui-server -v main.go
-docker build -t nginx-ui-demo .
-docker tag nginx-ui-demo uozi/nginx-ui-demo
-docker push uozi/nginx-ui-demo
+CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc \
+    CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags \
+    "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
+
+docker build -t nginx-ui .
+docker tag nginx-ui uozi/nginx-ui
+docker push uozi/nginx-ui

+ 16 - 0
demo.Dockerfile

@@ -0,0 +1,16 @@
+# CGO_ENABLED=1 GOOS=linux CC=x86_64-unknown-linux-gnu-gcc CXX=x86_64-unknown-linux-gnu-g++ GOARCH=amd64 go build -ldflags "-X 'github.com/0xJacky/Nginx-UI/server/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
+FROM --platform=linux/amd64 uozi/nginx-ui-base:latest
+WORKDIR /app
+EXPOSE 80
+
+COPY resources/demo/app.ini /etc/nginx-ui/app.ini
+COPY resources/demo/demo.db /etc/nginx-ui/database.db
+COPY resources/docker/nginx.conf /etc/nginx/nginx.conf
+COPY resources/docker/nginx-ui.conf /etc/nginx/conf.d/nginx-ui.conf
+COPY resources/docker/start.sh /app/start.sh
+COPY nginx-ui /app/nginx-ui
+
+RUN cd /app && chmod a+x start.sh
+
+
+ENTRYPOINT ["./start.sh"]

+ 1 - 1
frontend/version.json

@@ -1 +1 @@
-{"version":"1.4.1","build_id":3,"total_build":66}
+{"version":"1.4.1","build_id":4,"total_build":67}

BIN
nginx-ui


+ 0 - 4
resources/demo/sources.list

@@ -1,4 +0,0 @@
-deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
-deb http://mirrors.aliyun.com/debian-security buster/updates main
-deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
-deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

+ 0 - 3
resources/demo/start.sh

@@ -1,3 +0,0 @@
-#!/bin/bash
-nginx
-/app/nginx-ui --config app.ini

+ 6 - 1
resources/demo/nginx.conf → resources/docker/nginx-ui.conf

@@ -1,3 +1,8 @@
+map $http_upgrade $connection_upgrade {
+    default upgrade;
+    ''      close;
+}
+
 server {
     listen       80;
     server_name  localhost;  # your domain here
@@ -10,7 +15,7 @@ server {
         proxy_set_header   X-Forwarded-Proto    $scheme;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
-        proxy_set_header Connection upgrade;
+        proxy_set_header Connection $connection_upgrade;
         proxy_pass http://127.0.0.1:9000/;
     }
 }

+ 32 - 0
resources/docker/nginx.conf

@@ -0,0 +1,32 @@
+user  nginx;
+worker_processes  auto;
+
+error_log  /var/log/nginx/error.log notice;
+pid        /var/run/nginx.pid;
+
+
+events {
+    worker_connections  1024;
+}
+
+
+http {
+    include       /etc/nginx/mime.types;
+    default_type  application/octet-stream;
+
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+                      '$status $body_bytes_sent "$http_referer" '
+                      '"$http_user_agent" "$http_x_forwarded_for"';
+
+    access_log  /var/log/nginx/access.log  main;
+
+    sendfile        on;
+    #tcp_nopush     on;
+
+    keepalive_timeout  65;
+
+    gzip  on;
+
+    include /etc/nginx/conf.d/*.conf;
+    include /etc/nginx/sites-enabled/*;
+}

+ 10 - 0
resources/docker/start.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+if [ "$(ls -A /etc/nginx)" = "" ]; then
+    echo "Initialing Nginx config dir"
+    cp -rp /usr/etc/nginx/* /etc/nginx/
+    echo "Initialed Nginx config dir"
+fi
+
+nginx
+/app/nginx-ui --config /etc/nginx-ui/app.ini

+ 3 - 2
server/api/config.go

@@ -9,6 +9,7 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
+	"strings"
 )
 
 func GetConfigs(c *gin.Context) {
@@ -101,7 +102,7 @@ func AddConfig(c *gin.Context) {
 
 	output := nginx.ReloadNginx()
 
-	if output != "" {
+	if output != "" && strings.Contains(output, "error") {
 		c.JSON(http.StatusInternalServerError, gin.H{
 			"message": output,
 		})
@@ -147,7 +148,7 @@ func EditConfig(c *gin.Context) {
 
 	output := nginx.ReloadNginx()
 
-	if output != "" {
+	if output != "" && strings.Contains(output, "error") {
 		c.JSON(http.StatusInternalServerError, gin.H{
 			"message": output,
 		})

+ 3 - 2
server/api/domain.go

@@ -9,6 +9,7 @@ import (
 	"net/http"
 	"os"
 	"path/filepath"
+	"strings"
 )
 
 func GetDomains(c *gin.Context) {
@@ -120,7 +121,7 @@ func EditDomain(c *gin.Context) {
 
 		output := nginx.ReloadNginx()
 
-		if output != "" {
+		if output != "" && strings.Contains(output, "error") {
 			c.JSON(http.StatusInternalServerError, gin.H{
 				"message": output,
 			})
@@ -161,7 +162,7 @@ func EnableDomain(c *gin.Context) {
 
 	output := nginx.ReloadNginx()
 
-	if output != "" {
+	if output != "" && strings.Contains(output, "error") {
 		c.JSON(http.StatusInternalServerError, gin.H{
 			"message": output,
 		})

+ 1 - 0
server/tool/nginx/nginx.go

@@ -27,6 +27,7 @@ func ReloadNginx() string {
 
 	if err != nil {
 		log.Println(err)
+		return err.Error()
 	}
 
 	output := string(out)