Docker运行nginx文件服务器详细配置
nginx.conf
user root; worker_processes 1; error_log /var/log/nginx/error.log warn; 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; }
将nginx启动用户修改为root,否则会存在文件权限问题
nginx-file-server.conf
server { listen 8081; #端口 server_name localhost; #服务名 charset utf-8; # 避免中文乱码 root /data; #显示的根索引目录,注意这里要改成你自己的,目录要存在 location / { autoindex on; #开启索引功能 autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb) autoindex_localtime on; # 显示本机时间而非 GMT 时间 } }
启用8081作为文件服务器端口
运行命令:
docker run -d -p 8081:8081 --name file-server -v $(pwd):/data -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf -v $(pwd)/nginx-file-server.conf:/etc/nginx/conf.d/nginx-file-server.conf nginx
命令将宿主的当前目录挂载到容器的/data目录,并挂载conf配置文件,启动nginx
或者直接运行以下命令
curl -s https://files-cdn.cnblogs.com/files/nihaorz/start-nginx-file-server.sh | bash