nginx-unit docker 运行以及php &&golang 简单使用

备注:
   nginx  unit  nginx 开源的新的nginx 开发平台,但是说白了,个人感觉一般,而且官方文档也不是很好,
api 接口目前暂时文档比较。。。。,以前写过虚拟机&&物理机安装部署的方式,今天写一个使用docker
进行安装部署的方式,同时添加golang语言的使用,算是比较全了,毕竟大部分公司再用容器。
 
1. 参考项目
https://github.com/rongfengliang/nginx-unit-docker-demo
2. 项目说明
a. 项目结构

├── Dockerfile   // docker 构建
├── Jenkinsfile  // jenkins 构建
├── README.md
├── app.go   // golang 语言支持
├── docker-compose.yml  // docker-compose 构建
├── run.sh  // 应用配置,通过rest 接口
├── start.json  // 应用配置  php && golang 
└── website
    └── index.php  // php 语言


b. 代码说明(使用docker多阶段构建)

Dockerfile docker 构建

FROM golang:stretch AS build-env
RUN apt-get update
# RUN apt-get install -y build-essential
RUN git clone https://github.com/nginx/unit.git /go/src/github.com/nginx/unit

## 注意分之的版本 必须和你的unit 一致,不然报错,这个觉得不好,可能和目前生产可用性有关系吧

## 这个主要是golang sdk 的构建
RUN cd /go/src/github.com/nginx/unit && git checkout tags/1.1 && ./configure && ./configure go && make go-install

ADD . /go/src/github.com/rongfengliang/nginx-unit-go
RUN cd /go/src/github.com/rongfengliang/nginx-unit-go && go build -o /appdemo app.go

FROM nginx/unit:1.1-full
WORKDIR /appdemo
ADD start.json  /appdemo/start.json 
COPY website /appdemo/website/
COPY --from=build-env /appdemo /appdemo/website/appdemo
EXPOSE 8080 8000 8081
CMD ["/usr/sbin/unitd", "--no-daemon","--control", "0.0.0.0:8000"]

Jenkinsfile  jenkinspipeline  构建

pipeline {
  agent {
    node {
      label 'docker-65'
    }
  }
  stages {
    stage('docker-compose build') {
      steps {
        sh 'docker-compose build'
      }
    }
    stage('docker-compose run ') {
      steps {
        sh 'docker-compose up -d'
      }
    }
    stage('deploy service') {
      steps {
        sh './run.sh'
      }
    }
  }
}

app.go  golang 支持

package main

import (
	"math"
	"net/http"
	"nginx/unit" ## 注意unit 版本,感觉做的有点不好,因为版本因为无法发布服务
)

func term(ch chan float64, k float64) {
	ch <- 4 * math.Pow(-1, k) / (2*k + 1)
}

func handler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("appdemorong"))
}

func main() {
	http.HandleFunc("/", handler)
	//http.ListenAndServe(":8080", nil)
	unit.ListenAndServe(":8089", nil)
}


docker-compose.yml  比较简单,不用说了

version: "3"
services: 
  nginx-unit:
    build:
      context: .
    image: myunit
    ports: 
     - "9080:8080"
     - "9000:8000" 
     - "9001:8081"

run.sh   服务注册的shell
#!/bin/sh
curl -X PUT -d @$PWD/start.json http://localhost:9000

start.json  服务配置的说明

{
    "listeners":{
        "*:8080":{
            "application":"phpindex"
        },
        "*:8081":{
            "application":"golang"
        }
    },
    "applications":{
        "phpindex":{
           "type":"php",
           "processes":30,
           "root":"/appdemo/website",
           "index":"index.php"
        },
        "golang":{
            "type":"go",
            "processes":30,
            "working_directory": "/appdemo/website",
            "executable": "appdemo"
         }
    }
}

website/index.php  php  语言支持

<?php
 phpinfo()
?>
3. 构建&&启动
docker-compose build
docker-compose up -d
4. 发布服务
./run.sh
5. 效果
 
a. 注册的服务列表
b. golang 服务访问
 
c.  php 服务访问
 
6. 扩展
多版本切换以及多版本也比较简单,就是服务注册配置文件的处理,总的来说,挺好的功能,但是在微服务,以及service mesh 的大趋势之下,就显得没有那么好的,
也只是个人的观点
7. 参考资料
http://unit.nginx.org/
https://github.com/rongfengliang/nginx-unit-docker-demo

posted on   荣锋亮  阅读(584)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2016-05-11 unity 4 Please check your configuration file and verify this type name.

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示