nginx用dockerfile封装带nginx-module-vts的镜像

准备初始化镜像

nginx:1.27.2
https://download.csdn.net/download/qq_37579157/90460903
一、逻辑:
1.载官方基础镜像,安装依赖和编译的工具
2.下载nginx-module-vts,(需要和nginx的版本对应上,不然会报错),我这里直接用git拉取失败,所以手动从git上下载下来,再解压后复制进去
3.下载nginx部署包,因为自带的镜像编译工具删掉了,所以需要自己准备
4.编译打包

二、具体操作
1.Dockerfile内容

# 使用官方的 Nginx 镜像作为基础镜像
FROM nginx:1.27.2
 
# 设置工作目录
WORKDIR /usr/src/app
 
# 安装依赖和编译工具
RUN apt-get update && apt-get install -y \
    wget \
    build-essential \
    libpcre3 \
    libpcre3-dev \
    zlib1g-dev \
    libssl-dev \
    git
 
# 获取 vhost_traffic_status 模块源代码
#RUN git clone https://github.com/vozlt/nginx-module-vts.git /usr/src/nginx-module-vts
COPY nginx-module-vts-master /usr/src/nginx-module-vts
 
# 下载 Nginx 源码(确保版本与你的 Nginx 版本兼容)
RUN wget http://nginx.org/download/nginx-1.27.3.tar.gz -O /usr/src/nginx-1.27.3.tar.gz
RUN tar -zxvf /usr/src/nginx-1.27.3.tar.gz -C /usr/src
 
# 编译 Nginx 并添加 vhost_traffic_status 模块
RUN cd /usr/src/nginx-1.27.3 && \
    ./configure --prefix=/etc/nginx \
                --sbin-path=/usr/sbin/nginx \
                --modules-path=/usr/lib/nginx/modules \
                --conf-path=/etc/nginx/nginx.conf \
                --error-log-path=/var/log/nginx/error.log \
                --http-log-path=/var/log/nginx/access.log \
                --pid-path=/var/run/nginx.pid \
                --lock-path=/var/run/nginx.lock \
                --http-client-body-temp-path=/var/cache/nginx/client_temp \
                --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
                --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
                --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
                --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
                --user=nginx \
                --group=nginx \
                --with-http_ssl_module \
                --with-http_v2_module \
                --add-module=/usr/src/nginx-module-vts && \
    make && make install
 
# 删除不需要的文件以减小镜像大小
RUN rm -rf /usr/src/*
 
# 配置 Nginx 以使用 vhost_traffic_status(示例配置)
#COPY nginx.conf /etc/nginx/nginx.conf
#COPY vhost_traffic_status.conf /etc/nginx/conf.d/vhost_traffic_status.conf
#COPY default.conf /etc/nginx/conf.d/default.conf

2.打包

docker build . -t nginx-monitor:1.27.2

3.nginx.conf配置


user root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       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;

    keepalive_timeout  65;

    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_set_key $uri uris::$server_name;
    server {
        listen 9900;
        vhost_traffic_status on;
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
    
        }
    }
}

4.测试启动

docker run -itd -v /data/nginx.conf:/etc/nginx/nginx.conf -p 9900:9900 --restart=always  nginx-monitor:1.27.2

nginx用dockerfile封装带nginx-module-vts的镜像_第1张图片
成功!

你可能感兴趣的:(nginx,linux,docker)