侧边栏壁纸
博主头像
千异博主等级

学无止境,学以致用,志存高远!

  • 累计撰写 29 篇文章
  • 累计创建 26 个标签
  • 累计收到 0 条评论

docker安装nginx

千异
2022-04-22 / 0 评论 / 0 点赞 / 526 阅读 / 2,519 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

拉取镜像,启动一个容器

docker pull nginx:latest
docker images

docker run --name nginx -p 8080:80 -d nginx

把容器内默认配置拷贝出来,并删除容器

sudo docker cp nginx:/etc/nginx/nginx.conf /home/suixin/.nginx/nginx.conf
sudo docker cp nginx:/etc/nginx/conf.d /home/suixin/.nginx/conf.d
sudo docker cp nginx:/usr/share/nginx/html /home/suixin/.nginx/html
#sudo docker cp nginx:/var/log/nginx /home/suixin/.nginx/logs

docker rm -f nginx

编辑配置

cd /home/suixin/.nginx/conf/conf.d
sudo cp default.conf qianyi.conf
sudo vim qianyi.conf

nginx配置

server {
    listen       80;
    listen  [::]:80;
    server_name  www.qianyi.xin qianyi.xin;
    access_log off;
    location /{
#        proxy_pass http://www.qianyi.xin:7080;
      return 301 https://www.qianyi.xin$request_uri;
    }
}

server {
    listen       80;
    listen  [::]:80;
    server_name  www.jiangqiang.xin jiangqiang.xin;
    access_log off;
    location /{
#        proxy_pass http://www.qianyi.xin:7080;
      return 301 https://www.jiangqiang.xin$request_uri;
    }
} 

server {
    listen       443 ssl;
    server_name  www.qianyi.xin qianyi.xin;
    ssl_certificate /etc/nginx/conf.d/ssl/7632472_www.qianyi.xin.pem;  # ssl 证书目录
    ssl_certificate_key /etc/nginx/conf.d/ssl/7632472_www.qianyi.xin.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;


    location / {
       proxy_pass http://www.qianyi.xin:7080;
    }

    #error_page  404              /404.html;
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
    #    root   /usr/share/nginx/html;
    #}
}

server {
    listen       443 ssl;
    server_name  www.jiangqiang.xin jiangqiang.xin;
    ssl_certificate /etc/nginx/conf.d/ssl/7632873_www.jiangqiang.xin.pem;  # ssl 证书目录
    ssl_certificate_key /etc/nginx/conf.d/ssl/7632873_www.jiangqiang.xin.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;


    location / {
       proxy_pass http://www.qianyi.xin:7080;
    }

    #error_page  404              /404.html;
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
    #    root   /usr/share/nginx/html;
    #}
}

运行容器

#日志文件挂载到宿主机会报错
docker run -it -p 8080:80 --name nginx -d --restart=unless-stopped -v /home/suixin/.nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/suixin/.nginx/conf.d:/etc/nginx/conf.d -v /home/suixin/.nginx/html:/usr/share/nginx/html -v /home/suixin/.nginx/logs:/var/log nginx

docker run -it -p 8080:80 -p 8443:443 --name nginx -d --restart=unless-stopped -v /home/suixin/.nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/suixin/.nginx/conf.d:/etc/nginx/conf.d -v /home/suixin/.nginx/html:/usr/share/nginx/html nginx

 #root用户启动
docker run -it -p 80:80 -p 443:443 --name nginx -d --restart=unless-stopped -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/conf.d:/etc/nginx/conf.d -v /root/nginx/html:/usr/share/nginx/html nginx
0
博主关闭了所有页面的评论