centos使用docker安装nginx,实现多项目部署

8 篇文章 0 订阅
1 篇文章 0 订阅
  1. 拉取最新的nginx镜像

docker pull nginx
  1. 创建宿主机上的映射文件目录,并授权

[root@1-0001 ~]# mkdir -p /opt/nginx/html
[root@1-0001 ~]# mkdir -p /opt/nginx/conf.d
[root@1-0001 ~]# mkdir -p /opt/nginx/logs
[root@1-0001 ~]# chmod -R +777 /opt/nginx
  1. 启动一个不挂载的容器

-d 后台启动

docker run -d --name nginx -p 80:80 nginx
  1. 从容器中将配置文件复制到宿主机中

[root@1-0001 ~]# docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/nginx.conf
[root@1-0001 ~]# docker cp nginx:/etc/nginx/conf.d /opt/nginx/conf.d/
[root@1-0001 ~]# docker cp nginx:/usr/share/nginx/html /opt/nginx/html/
  1. 删除未挂载容器

[root@1-0001 ~]# docker stop nginx
[root@1-0001 ~]# docker rm nginx
# 直接删除正在运行的容器
[root@1-0001 ~]# docker rm -f nginx
  1. 启动挂载容器

[root@1-0001 ~]# docker run --name nginx -p 8400:8400 -v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/conf.d:/etc/nginx/conf.d -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/html:/usr/share/nginx/html -d nginx
  1. 文件配置

将前端项目dist和dist1分别放到/opt/nginx/html目录下

[root@1-0002 html]# ls
50x.html  dist  dist1  dist1.zip  dist.zip  index.html
[root@1-0002 html]# pwd
/opt/nginx1/html

修改后的配置文件/opt/nginx/conf.d/default.conf,如下

server {
    listen       8400;
    listen  [::]:8400;
    server_name  localhost;
    root /usr/share/nginx/html;

    #access_log  /var/log/nginx/host.access.log  main;

    location /a {
        alias   /usr/share/nginx/html/dist;
        index  index.html index.htm;
        try_files $uri $uri/  /dist/index.html;
    }

    location /cfd {
        alias   /usr/share/nginx/html/dist1;
        index  index.html index.htm;
        try_files $uri $uri/  /dist1/index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  1. 重启容器

[root@1-0002 html]# docker restart nginx
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫雪群飘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值