在同一目錄準(zhǔn)備下面3個(gè)文件
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; #服務(wù)名
# 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; # 關(guān)閉計(jì)算文件確切大小(單位bytes)斜友,只顯示大概大谢摇(單位kb饰躲、mb嬉橙、gb)
autoindex_localtime on; # 顯示本機(jī)時(shí)間而非 GMT 時(shí)間
}
}
執(zhí)行sh start-nginx.sh
測(cè)試一下:
echo file_server > data/file1.txt
打開瀏覽器 http://127.0.0.1:8081/