docker搭建nginx实战

版本说明:

Debian 10.6 Buster

Docker 20.10

Nginx 1.20.2



1、在文件夹(根据自己服务器具体架构创建)下新建docker-env文件夹。这里面包含nginx和php的镜像构建的所有文件和配置,以及Dockerfile

mkdir docker-env &&  cd docker-env  #创建 docker-env 文件夹  进入 docker-env 文件夹

mkdir nginx && cd nginx          #创建存放nginx相关文件文件夹 进入nginx文件夹

mkdir conf && mkdir logs && mkdir html  #创建nginx的配置目录和日志等目录

touch Dockerfile     # 创建Dockerfile文件


ls -l 查看创建好的目录


2、编辑nginx的Dockerfile文件。

vim Dockerfile

nginx的Dockerfile内容如下

# debian10 nginx1.20.2

# 基础镜像

FROM debian:10

# 维护者

LABEL maintainer="samger <353427880@qq.com>"

# 安装wget下载工具

# RUN apt-get install wget

# 切换nginx安装目录

WORKDIR /usr/local/src

# 添加远程文件到当前文件夹, 注意:后面有个点(.) 代表当前目录。ADD可以添加远程文件到镜像,但COPY仅可以添加本地文件到镜像中。

ADD http://nginx.org/download/nginx-1.20.2.tar.gz .

# RUN,在镜像内运行解压命令

RUN tar zxvf nginx-1.20.2.tar.gz

# 切换nginx目录下

WORKDIR /usr/local/src/nginx-1.20.2

RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" > /etc/apt/sources.list && \

    echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> /etc/apt/sources.list  && \

    echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list && \

    echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >> /etc/apt/sources.list && \

    echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list && \

    echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> /etc/apt/sources.list && \

    echo "deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list && \

    echo "deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> /etc/apt/sources.list

# 更新apt

RUN apt-get clean

RUN apt-get update

# 安装必要的软件和添加nginx用户

RUN apt-get -y install build-essential

RUN apt-get -y install libpcre3 libpcre3-dev

RUN apt-get -y install openssl libssl-dev

RUN apt-get -y install zlib1g-dev

RUN apt-get -y install libxml2 libxml2-dev libxslt-dev

RUN apt-get -y install libgd-dev

RUN useradd -M -s /sbin/nologin nginx

# 挂载卷,测试用例(这里的挂载卷,最好不要直接指定本机的目录,不灵活,一般会在 启动容器时通过 -v 参数指定挂载卷,或在docker-compose.yaml文件中指定,都可以指定本地目录)

# VOLUME ["/pan"]

# 编译安装nginx  --with-http_geoip_module

RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_degradation_module --with-http_stub_status_module && make && make install

# 切换到Nginx的配置目录

WORKDIR /usr/local/nginx/conf

# 建立子配置文件夹,个人爱好,可以不建,或者叫其它名称都可以,但最好不要带特殊符号,

RUN mkdir vhost

# 设置变量,执行命令时,就可以省略前缀目录了

ENV PATH /usr/local/nginx/sbin:$PATH

# 暴露端口

EXPOSE 80

# the command of entrypoint

ENTRYPOINT ["nginx"]

# 执行命令,数组形式, "-g daemon off;" 使我们运行容器时,容器可以前台运行,不会退出

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

编辑完Dockerfile文件

:wq  #保存退出


3、将我们的Dockerfile文件构建成nginx镜像

docker build -t  nginx_debian:1.0 .   #注意,最后有个点(表示生成的镜像放到当前目录下)! 1.0代表我们定义的版本号

成功后查看镜像 docker images 。可以看到,镜像列表中多了我们刚才build的镜像。

4、配置nginx相关

进入nginx的conf目录(注意:此处的 /pan 需要对于自己服务器具体的真实路径)

cd /data/docker-env/nginx/conf

 创建并编辑nginx.conf

touch nginx.conf && vim nginx.conf

把下方的配置复制进nginx.conf配置文件

user nginx;

worker_processes  1;

error_log  /usr/local/nginx/logs/error.log warn;

pid        /usr/local/nginx/logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include      /usr/local/nginx/conf/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  /usr/local/nginx/logs/access.log  main;

    server_names_hash_bucket_size 128;

        client_header_buffer_size 32k;

        large_client_header_buffers 4 32k;

        client_max_body_size 50m;

        sendfile  on;

        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;

        fastcgi_send_timeout 300;

        fastcgi_read_timeout 300;

        fastcgi_buffer_size 64k;

        fastcgi_buffers 4 64k;

        fastcgi_busy_buffers_size 128k;

        fastcgi_temp_file_write_size 256k;

        gzip on;

        gzip_min_length  1k;

        gzip_buffers    4 16k;

        gzip_http_version 1.1;

        gzip_comp_level 2;

        gzip_types    text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

        gzip_vary on;

        gzip_proxied  expired no-cache no-store private auth;

        gzip_disable  "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;

        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;

        access_log off;

    include /usr/local/nginx/conf/vhost/*.conf;

    server {

        listen      80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root  html;

            index  index.html index.htm;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page  500 502 503 504  /50x.html;

        location = /50x.html {

            root  html;

        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass  http://127.0.0.1;

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root          html;

        #    fastcgi_pass  127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen      8000;

    #    listen      somename:8080;

    #    server_name  somename  alias  another.alias;

    #    location / {

    #        root  html;

    #        index  index.html index.htm;

    #    }

    #}

    # HTTPS server

    #

    #server {

    #    listen      443 ssl;

    #    server_name  localhost;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

    #    location / {

    #        root  html;

    #        index  index.html index.htm;

    #    }

    #}

}

编辑完nginx.conf文件

:wq  #保存退出


5、创建容器(注意:此处的 /pan 需要对于自己服务器具体的真实路径)

docker run -d -p 80:80 \

-v /data/docker-env/nginx/logs:/usr/local/nginx/logs \

-v /data/docker-env/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf \

-v /data/docker-env/nginx/conf/vhost:/usr/local/nginx/conf/vhost \

-v /data/docker-env/www:/www nginx_debian:1.0


此时,运行 docker ps 命令就能看到咱们的nginx容器已经跑起来了

重启nginx

docker exec -it 容器ID nginx -s reload


之后项目文件放在宿主机的www文件下,nginx的配置放在vhost文件下即可。










123

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容