nginx文件服务器

如果实际目录是 /opt/archive/jupyter/,而你希望访问 http://127.0.0.1:31800/helloWeb/jupyter/ 时能够映射到该目录并下载文件,可以通过调整 rootalias 指令来实现。

修改后的 Nginx 配置

nginx
复制
location /helloWeb/jupyter/ {
    alias /opt/archive/jupyter/;  # 使用 alias 映射到实际目录
    autoindex on;
    autoindex_format html;
    autoindex_exact_size off;
    autoindex_localtime on;
    default_type application/octet-stream;

    # 强制下载文件
    if ($request_filename ~* ^.*?\.(txt|csv|doc|docx|xlsx|xls|pdf|rar|gz|zip|exe|ppt|pptx)$) {
        add_header Content-Disposition "attachment;";
    }

    sendfile on;
    sendfile_max_chunk 10m;
    tcp_nopush on;
    # aio on;
    directio 5m;
    directio_alignment 4096;
    output_buffers 4 32k;
    # limit_rate 1m;
    # limit_rate_after 2m;
    max_ranges 4096;
    send_timeout 20s;
    postpone_output 2048;
    chunked_transfer_encoding on;
}
 

修改说明

  1. alias 指令
    • 使用 alias /opt/archive/jupyter/;/helloWeb/jupyter/ 映射到实际的 /opt/archive/jupyter/ 目录。
    • aliasroot 的区别在于:
      • root 是在请求路径的基础上拼接目录路径。
      • alias 是直接将请求路径替换为指定的目录路径。
    • 在你的场景中,alias 更适合,因为它可以将 /helloWeb/jupyter/ 直接映射到 /opt/archive/jupyter/
  2. 其他配置保持不变
    • 其他配置项(如 autoindexContent-Disposition 等)保持不变,用于支持目录浏览和文件下载。

注意事项

  1. 检查路径权限
    • 确保 Nginx 用户(通常是 www-datanginx)有权限访问 /opt/archive/jupyter/ 目录。
  2. 重新加载 Nginx 配置
    • 修改配置后,运行以下命令检查配置文件语法是否正确:
      bash
      复制
      sudo nginx -t
       
    • 如果配置文件没有问题,重新加载 Nginx 配置:
      bash
      复制
      sudo nginx -s reload
       
  3. 测试访问
    • 访问 http://127.0.0.1:31800/helloWeb/jupyter/,应该能够看到 /opt/archive/jupyter/ 目录下的文件列表,并且可以下载文件。
通过这种方式,即使目录 /opt/archive/helloWeb/jupyter/ 不存在,也可以通过 alias 将其映射到实际的 /opt/archive/jupyter/ 目录。
posted @ 2025-04-28 20:27  牧之丨  阅读(59)  评论(0)    收藏  举报