1、准备软件包及配置文件
.
├── conf
│ ├── nginx.conf
│ └── proxy.conf
├── docker-compose.yml
├── Dockerfile
├── nginx-1.18.0.tar.gz
├── nginx-sticky-module-1.1.tar.gz
├── openssl-1.1.1k.tar.gz
├── pcre-8.44.tar.gz
├── run.sh
└── zlib-1.2.11.tar.gz
1 directory, 10 files
注意,这里准备的软件包 nginx-sticky-module-1.1.tar.gz 是修改过的。如果是从互联网上下载的,请参考我的另一篇文章《在 Linux 系统下安装和配置 Nginx·中篇》,因为 sticky 模块已经很久没有更新了,新版本 nginx 在编译时会有问题。因此,需要对 sticky 源码进行修改。
2、docker-compose.yml 文件
services:
nginx:
container_name: nginx
hostname: RicenOS
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- 80:80
- 443:443
volumes:
- /opt/nginx.conf:/usr/local/nginx/conf/vhosts
- /opt/websites:/usr/local/nginx/html
- /opt/weblogs:/usr/local/nginx/logs
3、Dockerfile 文件
FROM docker-registry:5000/centos:7
# 安装依赖包
RUN yum install gcc gcc-c++ glibc make perl file -y
# 准备软件包
ADD nginx-1.18.0.tar.gz /opt/softwares/
# 上传依赖包
ADD nginx-sticky-module-1.1.tar.gz openssl-1.1.1k.tar.gz pcre-8.44.tar.gz zlib-1.2.11.tar.gz /opt/softwares/nginx-1.18.0/
# 切到 pcre 安装目录
WORKDIR /opt/softwares/nginx-1.18.0/pcre-8.44
# 配置、编译 pcre
RUN ./configure --enable-utf8 && make
# 切到 openssl 安装目录
WORKDIR ../openssl-1.1.1k
# 配置、编译 openssl
RUN ./config && make
# 切到 zlib 安装目录
WORKDIR ../zlib-1.2.11
# 配置、编译 zlib
RUN ./configure && make
# 切回到 nginx 安装目录
WORKDIR ../
# 配置、编译、安装 nginx
RUN ./configure --prefix=/usr/local/nginx \
--with-http_realip_module --with-http_sub_module --with-http_flv_module \
--with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module \
--with-http_addition_module --with-http_ssl_module --add-module=nginx-sticky-module-1.1 \
--with-pcre=pcre-8.44/ --with-openssl=openssl-1.1.1k/ --with-zlib=zlib-1.2.11/ && make && make install
# 配置环境变量
ENV NGINX_HOME /usr/local/nginx
ENV PATH $NGINX_HOME/sbin:$PATH
# 暴露端口
EXPOSE 80
EXPOSE 443
# 添加配置文件
COPY conf/nginx.conf $NGINX_HOME/conf/nginx.conf
ADD conf/proxy.conf $NGINX_HOME/conf/proxy.conf
# 添加启动脚本
ADD run.sh /run.sh
# 给启动脚本授权
RUN chmod 755 /run.sh
# 启动 nginx
CMD ["/run.sh"]
这里用到的基础镜像“docker-registry:5000/centos:7”是我从私有仓库中导出的,有关私有仓库的搭建,详见我的另一篇文章《docker 搭建本地 Registry》。
4、conf/nginx.conf 文件
user daemon daemon;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
multi_accept on;
accept_mutex on;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '[$time_local] - $remote_addr $http_host $http_x_forwarded_for '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time';
access_log off;
#server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
server_names_hash_bucket_size 512;
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
client_max_body_size 2000M;
client_body_buffer_size 128K;
types_hash_max_size 2048;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include vhosts/*.conf;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/css application/javascript text/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;
gzip_vary on;
}
daemon off;
这里要加上“daemon off;”是因为 Docker 容器启动时,默认会把容器内部第一个进程,也就是 pid=1 的程序作为 docker 容器是否正在运行的依据。如果 docker 容器 pid=1 的进程挂了,那么 docker 容器就会直接退出。
Docker 未执行自定义的 CMD 之前,nginx 的 pid 是 1,执行到 CMD 后,nginx 就在后台运行,bash 或 sh 脚本的 pid 变成了 1。所以,一旦执行完自定义 CMD,nginx 容器也就退出了。
为了保持 nginx 容器不会退出,应该关闭 nginx 的后台运行模式,即以非守护进程模式运行。
5、conf/proxy.conf 文件
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Host $host:$server_port;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 1024M;
6、run.sh 文件
/usr/local/nginx/sbin/nginx
7、准备 Docker 数据卷
/opt/websites:用来存放静态网页数据
/opt/weblogs:用来存放网站日志文件
8、准备测试数据
虚拟主机配置文件:
[root@docker-client nginx]# cat /opt/nginx.conf/127.0.0.1.conf
server {
listen 80;
server_name 127.0.0.1;
access_log logs/127.0.0.1_access.log main;
error_log logs/127.0.0.1_error.log error;
location / {
index index.htm index.html;
root html;
}
}
虚拟主机测试页面:/opt/websites/index.html
9、构建和运行 nginx
注意,这里要确保联网。因为在 Dockerfile 文件中使用了 yum 命令安装相关的依赖包。
10、查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5efd21909bef nginx_nginx "/run.sh" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp nginx
由于已经做了端口映射,可以直接在浏览器中访问:
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.