在同一目录准备下面3个文件
start-nginx.sh
#!/bin/bash
mkdir data
docker stop nginx_file_server
docker rm nginx_file_server
docker run -d -p 8081:8080\
--name nginx_file_server \
-v $(pwd)/data:/data \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf \
-v $(pwd)/default.conf:/etc/nginx/conf.d/default.conf \
nginx:stable-alpine
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;
}
default.conf
server {
listen 8080; #端口
server_name localhost; #服务名
# for SSL listen port only
#ssl_certificate /etc/nginx/conf.d/server.pem;
#ssl_certificate_key /etc/nginx/conf.d/server-key.pem;
#ssl_protocols TLSv1.2;
#ssl_prefer_server_ciphers on;
#ssl_session_timeout 5m;
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#underscores_in_headers on;
charset utf-8; # 避免中文乱码
root /data; #显示的根索引目录,注意这里要改成你自己的,目录要存在
location / {
autoindex on; #开启索引功能
autoindex_exact_size off; # 关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; # 显示本机时间而非 GMT 时间
}
}
执行sh start-nginx.sh
测试一下:
echo file_server > data/file1.txt
打开浏览器 http://127.0.0.1:8081/