1、直接安装最新的nginx
docker pull nginx
2、由于后期需要方便配置与管理nginx,需要把nginx容器内的文件夹进行挂载到宿主机中,所以此处需要进行到自己心仪的盘中创建文件夹(本次说明在/home)
mkdir nginx && cd $_ && mkdir -p {ssl,config,logs}
ssl放域名对应证书
config放nginx配置文件
logs放nginx日志
3、先启动一个nginx容器用于cp对应的文件夹类型,用于后期挂载使用
docker run --name nginx -p 80:80 -d nginx
4、进行容器文件cp
nginx容器内部文件夹详情
docker cp nginx:/etc/nginx/nginx.conf /home/nginx/config/
docker cp nginx:/etc/nginx/conf.d /home/nginx
docker cp nginx:/usr/share/nginx/html /home/nginx
5、此时需要停止并删除已启动容器
docker stop nginx
docker rm nginx
6、把我们域名对应的证书放置ssl文件夹中
7、现在开始配置一下我们的nginx.conf文件了(注意:本例子80端口强制跳转到443,即强https)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
client_max_body_size 100m;
include mime.types;
server {
listen 80;
server_name www.xxx.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl; # 1.1版本后这样写
server_name www.xxx.com; #填写绑定证书的域名
ssl_certificate /etc/nginx/ssl/xxx.com.pem; # 指定证书的位置,绝对路径
ssl_certificate_key /etc/nginx/ssl/xxx.com.key; # 绝对路径,同上
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:1m;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
}
# 引入扩展配置(可以细分服务nginx)
include /etc/nginx/conf.d/*.conf;
}
8、此时可以输入命令进行启动容器
docker run --name nginx -p 443:443 -p 80:80 -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/config/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/logs:/var/log/nginx -v /home/nginx/ssl:/etc/nginx/ssl --privileged=true -d --restart=always nginx
9、此时启动成功完毕
10、浏览器输入对应域名或者ip即可访问
11、修改配置nginx配置文件后加载
docker exec 容器id nginx -t
docker exec 容器id nginx -s reload