yum install docker -y
docker pull nginx
docker run --name nginx -p 81:80 -d nginx
–name nginx 代表为启动后的容器命名nginx -p 81:80为将docker容器内的80端口映射到宿主机的81端口 -d nginx 后台运行nginx 浏览器访问宿主机ip:81看到welcome to nginx的页面样式即代表启动成功 如果未成功请关闭防火墙或者开放81断开
systemctl stop firewalld #关闭防火墙
systemctl start firewalld #开启防火墙
systemctl status firewalld #查看防火墙状态
firewall-cmd --zone=public --add-port=81/tcp --permanent # 开放81端口
firewall-cmd --zone=public --remove-port=81/tcp --permanent #关闭81端口
firewall-cmd --reload # 配置立即生效
mkdir -p /root/docker/nginx/{conf,html,logs}
docker cp nginx:/etc/nginx/nginx.conf /root/docker/nginx
dokcer cp nginx:/etc/nginx/conf.d/default.conf /root/docker/nginx/conf
dokcer cp nginx:/etc/nginx/conf.d/default.conf /root/docker/nginx/conf
docker stop nginx #停止模板容器
docker rm nginx #删除模板容器
vim /root/docker/nginx/conf/default.conf #打开配置文件
按照自己的需求修改配置文件,我的配置如下:
server {
listen 80;
listen [::]:80;
server_name tanblog.cc;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name tanblog.cc;
ssl_certificate /etc/nginx/ssl/tanblog.cc_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/tanblog.cc.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
docker run -d --name nginx -p 80:80 -p 443:443 -v /root/docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/docker/nginx/logs:/var/log/nginx -v /root/docker/nginx/html:/usr/share/nginx/html -v /root/docker/nginx/conf:/etc/nginx/conf.d -v /root/docker/nginx/ssl:/etc/nginx/ssl --privileged=true -d nginx
文章到此结束,恭喜你已经成功使用搭建nginx并配置文件目录挂载!如有任何疑问可在文章下留言。