1、创建ssl.conf配置文件
cat ssl.conf
server {
listen 443 ssl;
server_name www.xxx.com; //写上域名
ssl_certificate /etc/nginx/ssl/nginx.key; //写上在Dockerfile里面的证书路径
ssl_certificate_key /etc/nginx/ssl/nginx.pem; //写上在Dockerfile里面的证书路径
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
2、从容器中拷贝default.conf文件加上自动跳转https
[root@taorui666 nginx]# vim default.conf
server {
listen 80;
listen [::]:80;
server_name www.xxx.com; //写域名
rewrite ^(.*) https://$server_name$1 permanent; //跳转
3、准备好证书和站点文件并编写Dockerfile
[root@taorui666 nginx]# cat Dockerfile
FROM nginx:stable
# copy the custom website into the image
COPY website /usr/share/nginx/html/
# copy the SSL configuration file into the image
COPY ssl.conf /etc/nginx/conf.d/ssl.conf
COPY default.conf /etc/nginx/conf.d/default.conf
# download the SSL key and certificate into the image
COPY nginx.key /etc/nginx/ssl/nginx.key
COPY nginx.pem /etc/nginx/ssl/nginx.pem
# expose the https port
EXPOSE 443
4、生成镜像
docker build -t test-nginx:1.0 .
5、运行容器
docker run -itd -p 443:443 -p 80:80 test-nginx:1.0 #映射端口