Centos7用Docker部署Nginx实现静态文件服务(个人经验总结)

1前期准备:

1.1创建挂载目录

  • nginx_logs :日志文件目录
  • nginx_conf :配置文件目录
  • nginx_web :项目文件目录(这里可以存放待加载的文件)
mkdir /root/nginx_conf
mkdir /root/nginx_logs
mkdir /root/nginx_web

1.2授权文件夹,防止nginx操作文件夹权限不足

chmod 777 /root/nginx_conf
chmod 777 /root/nginx_logs
chmod 777 /root/nginx_web

1.3拉取nginx镜像---这里我用的命名nginxystest,映射端口33154(避免使用默认80端口)

docker run --name nginxystest -p 33154:80 -d nginx

1.4复制容器配置文件到宿主机

docker cp nginxystest:/etc/nginx /root/nginx_conf

1.5删除容器

docker rm -f nginxystest

2开始搭建

2.1启动容器并挂载目录(注意:名称和端口因人而异)
  1. 第七行> -v /root/nginx_web:/root/nginx_web \这行的作用是将此目录挂载到docker里,否则运行会报404,注意注意!!!!!!!!!!

docker run \
-p 36521:80 \
--name nginx-ys-eap \
-v /root/nginx_html:/usr/share/nginx/html \
-v /root/nginx_conf/nginx:/etc/nginx \
-v /root/nginx_logs:/var/log/nginx \
-v /root/nginx_web:/root/nginx_web \
-d nginx:latest

2.2修改nginx.conf配置文件(将第一行改为:user root)

路径:/root/nginx_conf/nginx/nginx.conf

user  root;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

2.3修改default.conf配置文件

路径:/root/nginx_conf/nginx/conf.d/default.conf

改动: 

  charset utf-8;
    root   /root/nginx_web/;
    location / {
        autoindex on;
        autoindex_exact_size off; 
        autoindex_localtime on;
    }

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;
    charset utf-8;
    root   /root/nginx_web/;
    location / {
        autoindex on;
        autoindex_exact_size off; 
        autoindex_localtime on;
    }

    #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;
    #}
}

2.4重启nginx

docker restart nginx-ys-eap

3.效果展示

打开浏览器:输入ip+端口

  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值