nginx 文件服务器

🔧 ‌一、部署基础文件服务器‌

1. ‌创建本地目录‌

用于存放配置和文件:

bashCopy Code
 
mkdir -p /opt/nginx/conf /opt/nginx/html /opt/nginx/logs

▿ 目录作用:

  • /opt/nginx/conf:存放 Nginx 配置文件
  • /opt/nginx/html:存储共享文件(可自定义路径)
  • /opt/nginx/logs:日志目录

2. ‌配置 Nginx‌

创建配置文件 /opt/nginx/conf/default.conf,内容如下:

server {
    listen 80;
    server_name localhost;
    location / {
        root /usr/share/nginx/html;  # 容器内文件目录
        autoindex on;                 # 启用目录浏览
        autoindex_exact_size off;     # 显示友好文件大小(如KB/MB)
        autoindex_localtime on;       # 显示本地时间
    }
}

  

3. ‌启动容器‌

bashCopy Code
 
docker run -d \ --name nginx-fileserver \ -p 8030:80 \ # 主机端口:容器端口 -v /opt/nginx/conf:/etc/nginx/conf.d \ # 挂载配置 -v /opt/nginx/html:/usr/share/nginx/html \ # 挂载文件目录 -v /opt/nginx/logs:/var/log/nginx \ # 挂载日志 nginx:latest

✅ ‌验证访问‌:浏览器打开 http://服务器IP:8030 查看文件列表。‌23


️ ‌二、高级配置选项‌

1. ‌自定义文件根目录‌

修改挂载路径(如主机目录 /data/files):

bashCopy Code
 
-v /data/files:/usr/share/nginx/html

2. ‌启用 HTTPS‌

需额外:

  • 挂载 SSL 证书到容器内
  • 在配置中添加 listen 443 ssl 并指定证书路径
  • 开放 443 端口映射(如 -p 8443:443)‌57

3. ‌权限管理‌

  • ‌主机权限‌:确保挂载目录可读(如 chmod -R 755 /opt/nginx/html)‌1
  • ‌Nginx 权限‌:通过配置限制 IP 或密码访问(需添加 auth_basic 指令)‌5

‌三、部署优化建议‌

  1. ‌配置文件持久化‌
    首次启动时复制容器默认配置到主机:
bashCopy Code
 
docker run --name tmp-nginx -d nginx docker cp tmp-nginx:/etc/nginx/nginx.conf /opt/nginx/conf/nginx.conf docker rm -f tmp-nginx
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; # 容器内文件目录 autoindex on; # 启用目录浏览 autoindex_exact_size off; # 显示友好文件大小(如KB/MB) autoindex_localtime on; # 显示本地时间 } }
posted @ 2025-07-30 14:32  rincloud  阅读(123)  评论(0)    收藏  举报