关于nginx:国密Nginx容器实战

5次阅读

共计 6551 个字符,预计需要花费 17 分钟才能阅读完成。

国密 Nginx 容器实战

1. 背景

​ 目前国密 SSL(TLCP)曾经逐渐开始推广并理论应用,国密 SSL 实验室(www.gmssl.cn)提供了国密版 OpenSSL,并且能够与 Nginx 集成,能够比拟不便的搭建国密 SSL 反向代理或者国密 SSL 服务器。

​ 国密 SSL 实验室并没有提供 Docker 的样例,思考到 Docker 的广泛性,本文形容了在 Docker 外面部署国密 SSL 的 Nginx 的的残缺构建过程,仅供学习和参考之用。

运行环境为 Centos7 X86_64。

2. 装置 docker

# 装置 docker 17.03.0 
#装置 docker 依赖三个 yum 源:Base,Extras,docker-ce
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#从新生成 yum 缓存
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache

#装置 docker 所需依赖包
[root@localhost ~]# yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

#装置 docker
[root@localhost ~]# yum -y install docker-ce-17.03.0.ce-1.el7.centos

3. 配置镜像减速

[root@localhost ~]# mkdir /etc/docker
[root@localhost ~]# cat > /etc/docker/daemon.json<<EOF
> {>   "registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"]
> }
> EOF

[root@localhost ~]# systemctl enable --now docker

4. 筹备容器环境

# 拉取 centos 镜像
[root@localhost ~]# docker pull centos:centos7.9.2009

#运行容器
[root@localhost ~]# docker run -itd --name nginx centos:centos7.9.2009 
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@308fdeaaa058 /]# yum -y install wget gcc make pcre-devel
[root@308fdeaaa058 ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@308fdeaaa058 ~]# tar xf nginx-1.20.1.tar.gz

5. 编译 nginx

下载 gmssl_openssl_1.1_b4.tar.gz 与 nginx(参见资源与下载),而后编译。

[root@308fdeaaa058 ~]# exit
[root@localhost ~]# ls
anaconda-ks.cfg  gmssl_openssl_1.1_b4.tar.gz

#把下载好的 gmssl 包拷贝到容器里
[root@localhost ~]# docker cp gmssl_openssl_1.1_b4.tar.gz nginx:/root/
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@308fdeaaa058 /]# cd
[root@308fdeaaa058 ~]# tar xf gmssl_openssl_1.1_b4.tar.gz -C /usr/local/
[root@308fdeaaa058 ~]# cd nginx-1.20.1
[root@308fdeaaa058 nginx-1.20.1]# vi auto/lib/openssl/conf
#将将全副 $OPENSSL/.openssl/ 批改为 $OPENSSL/ 并保留

#编译配置
[root@308fdeaaa058 nginx-1.20.1]# ./configure \
> --without-http_gzip_module \
> --with-http_ssl_module \
> --with-http_stub_status_module \
> --with-http_v2_module \
> --with-file-aio \
> --with-openssl="/usr/local/gmssl" \
> --with-cc-opt="-I/usr/local/gmssl/include" \
> --with-ld-opt="-lm"

[root@308fdeaaa058 nginx-1.20.1]# make install
[root@308fdeaaa058 nginx-1.20.1]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

#创立证书和子配置文件目录
[root@308fdeaaa058 nginx-1.20.1]# cd /usr/local/nginx/conf/
[root@308fdeaaa058 conf]# vi nginx.conf
include /usr/local/nginx/conf/conf.d/*.conf;  #将此条增加到 http 里

[root@308fdeaaa058 conf]# mkdir ssl
[root@308fdeaaa058 conf]# mkdir conf.d
[root@308fdeaaa058 conf]# exit

6. 制作镜像

# 编译好的容器,打包成镜像
[root@localhost ~]# docker commit nginx nginx
sha256:742fe82c7114f8272883f3f6a528fc62498cd5044b5ef942acd0bb4014a03467
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              742fe82c7114        8 seconds ago       489 MB
centos              centos7.9.2009      8652b9f0cb4c        9 months ago        204 MB

#编写 Dockerfile
[root@localhost ~]# vim Dockerfile
[root@localhost ~]# cat Dockerfile
FROM nginx
LABEL version="1.0"
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]

#制作镜像
[root@localhost ~]# docker build -t "gmssl_nginx" .
Sending build context to Docker daemon 6.277 MB
Step 1/4 : FROM nginx
 ---> 742fe82c7114
Step 2/4 : LABEL version "1.0"
 ---> Running in efef92a82f76
 ---> b4305df3867a
Removing intermediate container efef92a82f76
Step 3/4 : EXPOSE 80 443
 ---> Running in c7addc9e5623
 ---> 63050d82a45a
Removing intermediate container c7addc9e5623
Step 4/4 : CMD nginx -g daemon off;
 ---> Running in ed1b4c9789c8
 ---> 8dfbfa5182f9
Removing intermediate container ed1b4c9789c8
Successfully built 8dfbfa5182f9
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
gmssl_nginx         latest              8dfbfa5182f9        About a minute ago   489 MB
nginx               latest              742fe82c7114        4 minutes ago        489 MB
centos              centos7.9.2009      8652b9f0cb4c        9 months ago         204 MB

7. 启动容器

生成国密双证书(参见资源与下载)并配置 nginx。

# 配置目录
[root@localhost ~]# cd conf.d/
[root@localhost conf.d]# cat ssl.conf 
server
{
  listen 0.0.0.0:443 ssl;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA:DES-CBC3-SHA:ECC-SM4-CBC-SM3:ECDHE-SM4-GCM-SM3;
  ssl_verify_client off;

  ssl_certificate /usr/local/nginx/conf/ssl/sm2.test.c.sig.crt.pem;
  ssl_certificate_key /usr/local/nginx/conf/ssl/sm2.test.c.sig.key.pem;

  ssl_certificate_key /usr/local/nginx/conf/ssl/sm2.test.c.enc.key.pem;
  ssl_certificate /usr/local/nginx/conf/ssl/sm2.test.c.enc.crt.pem;

  location /
  {
    root html;
    index index.html index.htm;
  }
}

[root@localhost conf.d]# cd

#证书目录
[root@localhost ~]# cd sm2.test.c/
[root@localhost sm2.test.c]# ls
password.txt  sm2.rca.pem          sm2.test.c.enc.cer      sm2.test.c.enc.key              sm2.test.c.enc.key.crypted.bin  sm2.test.c.enc.key.pem  sm2.test.c.sig.cer      sm2.test.c.sig.key     sm2.test.c.sig.key.pem
sm2.oca.pem   sm2.test.c.both.pfx  sm2.test.c.enc.crt.pem  sm2.test.c.enc.key.crypted.b64  sm2.test.c.enc.key.p8           sm2.test.c.enc.pfx      sm2.test.c.sig.crt.pem  sm2.test.c.sig.key.p8  sm2.test.c.sig.pfx
[root@localhost sm2.test.c]# cd

#启动容器
[root@localhost ~]# docker run -itd -p 443:443 -v /root/sm2.test.c/:/usr/local/nginx/conf/ssl/ -v /root/conf.d/:/usr/local/nginx/conf/conf.d --name gmssl_nginx gmssl_nginx
[root@localhost ~]# docker logs -f gmssl_nginx 
OpenSSL(GM version) by www.gmssl.cn. Test Only!!!
OpenSSL(GM version) by www.gmssl.cn. Test Only!!!
OpenSSL(GM version) by www.gmssl.cn. Test Only!!!
OpenSSL(GM version) by www.gmssl.cn. Test Only!!!

8. 应用国密浏览器拜访

下载奇安信国密浏览器(参见资源与下载)。

导入国密根证书:关上浏览器 > 设置 > 高级设置 > 证书治理 > 导入证书。导入证书链

批改本地解析:C:\Windows\System32\drivers\etc\hosts。hosts 文件中退出解析地址

关上浏览器拜访

9. 残缺的 Dockerfile

容器已上传 dockerhub(参见资源与下载)。

[root@localhost nginx]# ls
Dockerfile  gmssl_openssl_1.1_b4.tar.gz  nginx-1.20.1.tar.gz
[root@localhost nginx]# cat Dockerfile 
FROM centos:centos7.9.2009    #基于 centos7.9.2009 镜像
RUN yum install -y gcc make pcre-devel    #装置依赖环境包
ADD nginx-1.20.1.tar.gz /root/     #将源码复制到指定目录,并解压
ADD gmssl_openssl_1.1_b4.tar.gz /usr/local/
WORKDIR /root/nginx-1.20.1/    #为上面的指令指定执行目录
RUN sed -in "s/\.openssl\///g" auto/lib/openssl/conf && \
 ./configure --without-http_gzip_module --with-http_ssl_module --with-http_stub_status_module --with-http_v2_module --with-file-aio --with-openssl="/usr/local/gmssl" --with-cc-opt="-I/usr/local/gmssl/include" --with-ld-opt="-lm" && \
 make install && \
 mkdir /usr/local/nginx/conf/conf.d && \
 mkdir /usr/local/nginx/conf/ssl && \
 sed -i '$i  \    include /usr/local/nginx/conf/conf.d/*.conf;' /usr/local/nginx/conf/nginx.conf
EXPOSE 80 443       #申明裸露端口
VOLUME ["/usr/local/nginx/conf/conf.d/","/usr/local/nginx/conf/ssl/"]     #指定挂载点
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]     #为了放弃 nginx 的容器不退出,敞开 nginx 后盾运行

[root@localhost nginx]# docker build -t gmssl_nginx:v1 .    #应用当前目录下的文件构建标签 gmssl_nginx:v0 的镜像 

10. 资源与下载

1) 国密版 OpenSSL 下载:

https://www.gmssl.cn/gmssl/To…

2)Nginx 最新版下载:

http://nginx.org/download/ngi…

3) 国密双证书生成:

https://www.gmssl.cn/gmssl/in…

4) 奇安信国密浏览器下载:

https://www.gmssl.cn/gmssl/To…

5) 国密 Nginx 容器下载:

docker pull keyaas2021/gmssl_nginx:v1.0
正文完
 0