Docker 配置nginx文件上传的镜像

目录结构:

|-- docker
|   |-- Dockerfile
|   |-- install.sh
|   |-- nginx
|   |   |-- conf.d
|   |   |   `-- default.conf
|   |   `-- nginx.conf
|   `-- nginx.key
`-- docker-compose.yml

一、安装docker、docker-compose

1、不会安装docker的请移步----》Centos 7 安装docker  Ubuntu16.04安装Docker
2、安装docker-compose(Centos7环境无pip的----》centos7安装pip2)

pip install docker-compose

 第二、配置文件,构建镜像

 1、创建docker目录

mkdir docker && cd docker

2、创建nginx.key文件

cat << EOF > nginx.key
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQENBE7SKu8BCADQo6x4ZQfAcPlJMLmL8zBEBUS6GyKMMMDtrTh3Yaq481HB54oR
0cpKL05Ff9upjrIzLD5TJUCzYYM9GQOhguDUP8+ZU9JpSz3yO2TvH7WBbUZ8FADf
hblmmUBLNgOWgLo3W+FYhl3mz1GFS2Fvid6Tfn02L8CBAj7jxbjL1Qj/OA/WmLLc
m6BMTqI7IBlYW2vyIOIHasISGiAwZfp0ucMeXXvTtt14LGa8qXVcFnJTdwbf03AS
ljhYrQnKnpl3VpDAoQt8C68YCwjaNJW59hKqWB+XeIJ9CW98+EOAxLAFszSyGanp
rCqPd0numj9TIddjcRkTA/ZbmCWK+xjpVBGXABEBAAG0IU1heGltIERvdW5pbiA8
bWRvdW5pbkBtZG91bmluLnJ1PohGBBARAgAGBQJO01Y/AAoJEOzw6QssFyCDVyQA
n3qwTZlcZgyyzWu9Cs8gJ0CXREaSAJ92QjGLT9DijTcbB+q9OS/nl16Z/IhGBBAR
AgAGBQJO02JDAAoJEKk3YTmlJMU+P64AnjCKEXFelSVMtgefJk3+vpyt3QX1AKCH
9M3MbTWPeDUL+MpULlfdyfvjj4heBBARCAAGBQJRCTwgAAoJEFGFCWhsfl6CzF0B
AJsQ3DJbtGcZ+0VIcM2a06RRQfBvIHqm1A/1WSYmObLGAP90lxWlNjSugvUUlqTk
YEEgRTGozgixSyMWGJrNwqgMYokBHAQQAQgAB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P0dRGnK9Jc52wrKceaGn4Ul5T6jO7HlYvAEwwT43FS1503t8UGd
uYLAotNyP5rWzW75hUkaILENruiOX98mOpkjzJumycZUEomLaalnAMQBBKKIxMQD
EVBIqQH/AUDrQd8axS9F6rlWtjNzD6dgG+/XuQENBE7SKu8BCADQuMJzxLqhnOOJ
qnYyNE2FEUDkFiNdlFkF0oXZU/vhq+3b9EQurwZbSfslaYqfq++tdpsQYvJ+QKeA
CFOr20n8rss/H9uaU7n6Nug7/WKcfYFO0uIeMBe+JxdmTehpm3ZKi3HmYwjfLOkV
BFWpleyLQulRM8kSHehLwh1hGOyVCHUDs/mF8JnC3PBv+Eqby6k4N78hNQy5d5Z8
u5CP+4RTrAzOZ8wdv6OVgxa1v6/3xolrYU4N4o+ZYJctrtuWJenVJJ4E4R8iAQCa
MzpAaUPHmxmGvZKzbFI9F1nwOoPuiJFXOay1fVijETMLTJd9p58LtKAzScydpO9g
Q5gn0IffABEBAAGJAR8EGAECAAkFAk7SKu8CGwwACgkQUgqZk6HAUvjzjgf+OjyJ
wJxLp45a+d6fuWDQVwKjV8ibjBbe0SaRB1yFIcQIkOrumBOWg3rmnAHm0GhPjtzJ
g1kD15N8lWBFzvirlx7Eo5iswLcq4CpZvCpeGRWzSeRbGc4iJ1MdJe8Fl8MUDgh3
C173hOGvN/w+fxQRK51OKqtagIFeTZTJWyXTeAluVIkB4kZg896j5iPqbrc5Z7b7
9LZVOCZs9L9E5ufQB6DvK59/Sfn7K0S3YovnWA5ATa4jFMJrAevXY+yWU+Ruo+T8
9qAi218Qmqf1vQKuo8LcdER49vZown8K3D/voI7CfSa1d4hcsJO8GbOdd9owqWWy
puzLacInY/a4mM6e3A==
=UK9v
-----END PGP PUBLIC KEY BLOCK-----
EOF

3、创建install.sh文件

cat << EOF > install.sh
#!/bin/sh
set -e

# Base setup
addgroup -S nginx \
  && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
  && apk add --no-cache --virtual .build-deps \
    curl \
    gcc \
    gd-dev \
    geoip-dev \
    git \
    gnupg \
    libc-dev \
    libxslt-dev \
    linux-headers \
    make \
    openssh \
    openssl-dev \
    pcre-dev \
    perl-dev \
    unzip \
    zlib-dev \


# Install GPG keys
gpg --import /usr/src/nginx.key

CONFIG="\
        --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_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gunzip_module \
        --with-http_gzip_static_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_stub_status_module \
        --with-http_auth_request_module \
        --with-http_xslt_module=dynamic \
        --with-http_image_filter_module=dynamic \
        --with-http_geoip_module=dynamic \
        --with-http_perl_module=dynamic \
        --with-threads \
        --with-stream \
        --with-stream_ssl_module \
        --with-http_slice_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-file-aio \
        --with-http_v2_module \
        --with-ipv6 \
        --add-module=/usr/src/progress/nginx-upload-progress-module-master \
        --add-module=/usr/src/upload/nginx-upload-module \
    "
curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz
curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc  -o nginx.tar.gz.asc
gpg --batch --verify nginx.tar.gz.asc nginx.tar.gz
export GNUPGHOME="$(mktemp -d)"
rm -r "$GNUPGHOME" nginx.tar.gz.asc
mkdir -p /usr/src
tar -zxC /usr/src -f nginx.tar.gz
rm nginx.tar.gz
mkdir -p /usr/src/upload
cd /usr/src/upload
git clone https://github.com/vkholodkov/nginx-upload-module.git
cd nginx-upload-module
git checkout 2.255
mkdir -p /usr/src/progress
cd /usr/src/progress
curl -fSLO https://github.com/masterzen/nginx-upload-progress-module/archive/master.zip
unzip master.zip
cd /usr/src/nginx-$NGINX_VERSION
./configure $CONFIG --with-debug
make -j$(getconf _NPROCESSORS_ONLN)
mv objs/nginx objs/nginx-debug
mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so
mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so
mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so
mv objs/ngx_http_perl_module.so objs/ngx_http_perl_module-debug.so
./configure $CONFIG
make -j$(getconf _NPROCESSORS_ONLN)
make install
rm -rf /etc/nginx/html/
mkdir -p /etc/nginx/conf.d/
mkdir -p /usr/share/nginx/html/
install -m644 html/index.html /usr/share/nginx/html/
install -m644 html/50x.html /usr/share/nginx/html/
install -m755 objs/nginx-debug /usr/sbin/nginx-debug
install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so
install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so
install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so
install -m755 objs/ngx_http_perl_module-debug.so /usr/lib/nginx/modules/ngx_http_perl_module-debug.so
ln -s ../../usr/lib/nginx/modules /etc/nginx/modules
strip /usr/sbin/nginx*
strip /usr/lib/nginx/modules/*.so
rm -rf /usr/src/nginx-$NGINX_VERSION
rm -rf /usr/src/upload
rm -rf /usr/src/progress

# Bring in gettext so we can get `envsubst`, then throw
# the rest away. To do this, we need to install `gettext`
# then move `envsubst` out of the way so `gettext` can
# be deleted completely, then move `envsubst` back.
apk add --no-cache --virtual .gettext gettext
mv /usr/bin/envsubst /tmp/

runDeps="$(
    scanelf --needed --nobanner /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
        | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
        | sort -u \
        | xargs -r apk info --installed \
        | sort -u \
)"

apk add --no-cache --virtual .nginx-rundeps $runDeps
apk del .gettext
mv /tmp/envsubst /usr/local/bin/

# forward request and error logs to docker log collector
ln -sf /dev/stdout /var/log/nginx/access.log
ln -sf /dev/stderr /var/log/nginx/error.log
EOF

4、创建nginx配置文件

mkdir nginx && cd nginx
cat << EOF > nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
}
EOF
mkdir conf.d && cd conf.d
cat << EOF > default.conf
server {
    listen      8888;
 
    location / {
        expires -1;
        root /usr/share/nginx/html;
    }

    location /logs/ {
        expires -1;
        root /usr/share/nginx/static/;
    }
 

    location /upload {
        # upload large file, begin
        client_max_body_size 8000M;
        client_body_buffer_size 8000M;
        client_body_timeout 120;

        add_header X-Clacks-Overhead "GNU Terry Pratchett";
        add_header X-Clacks-Overhead "GNU Terry Pratchet";
        add_header Access-Control-Allow-Origin *;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        # upload large file end

        # After upload, pass altered request body to this django view
        upload_pass   /api/file/;

        # Store files to this directory
        # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
        upload_store /usr/share/nginx/static/www/file/_upload 1;        
        upload_store_access user:rw group:rw all:rw;

        # Set specified fields in request body
        upload_set_form_field $upload_field_name.name "$upload_file_name";
        upload_set_form_field $upload_field_name.content_type "$upload_content_type";
        upload_set_form_field $upload_field_name.path "$upload_tmp_path";

        # Inform backend about hash and size of a file
        upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
        upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";

        # Here is where you define additional fields to pass through to upload_complete
        upload_pass_form_field "^submit$|^description$";
        upload_pass_form_field "^name$";
        upload_pass_form_field "^terminal$";
        upload_cleanup 400-599;

    }
}
EOF

5、回到docker目录下创建Dockerfile文件

cat << EOF > Dockerfile
FROM alpine:3.4
ENV NGINX_VERSION 1.13.6

COPY install.sh /usr/src/
COPY nginx.key /usr/src/

RUN sh -x /usr/src/install.sh

COPY nginx /etc/nginx

# Create hashed temporary upload locations
RUN mkdir -p /usr/src/nginx/static/www/file/_upload/0 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/1 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/2 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/3 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/4 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/5 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/6 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/7 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/8 && \
    mkdir -p /usr/src/nginx/static/www/file/_upload/9 && \
    chmod 777 -R /usr/src/nginx/static/www/file/_upload

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]
EOF

文件创建完成之后对照一下docker目录下的文件结构

|-- Dockerfile
|-- install.sh
|-- nginx
|   |-- conf.d
|   |   `-- default.conf
|   `-- nginx.conf
`-- nginx.key

6、文件全部创建完毕后构建nginx:alpine镜像,在docker目录下执行下面命令:

docker build -t nginx:alpine .

第三、启动镜像

1、创建docker-compose.yml文件

cat << EOF > docker-compose.yml

version: "2"

services:
  nginx:
    container_name: nginx
    image: "nginx:alpine"
    #  env_file: prod.env
    volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
      - /data/nginx/static:/usr/share/nginx/static
      - /data/nginx/logs:/var/log/nginx
    ports:
      - "8888:8888"
    restart: always
EOF

2、创建数据储存目录

mkdir -p /data/nginx/static/www/file/_upload/{0..9}
cd /data/nginx && chmod -R 777 static/
mkdir -p /data/nginx/logs

3、启动容器

docker-compose up -d

构建完成

最后在default.conf配置文件中  转到后台处理的接口upload_pass   /api/file/; 

接口对应的类,如下所示:(自己配置自己需要的数据结构)

class FileUploadCompileAPIView(APIView):
    def post(self, request, *args, **kwargs):
        data = dict(request.data.dict())
        return XopsResponse(data)

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值