nginx文件服务器
如果实际目录是
/opt/archive/jupyter/,而你希望访问 http://127.0.0.1:31800/helloWeb/jupyter/ 时能够映射到该目录并下载文件,可以通过调整 root 和 alias 指令来实现。修改后的 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;
}
修改说明
-
alias指令-
使用
alias /opt/archive/jupyter/;将/helloWeb/jupyter/映射到实际的/opt/archive/jupyter/目录。 -
alias与root的区别在于:-
root是在请求路径的基础上拼接目录路径。 -
alias是直接将请求路径替换为指定的目录路径。
-
-
在你的场景中,
alias更适合,因为它可以将/helloWeb/jupyter/直接映射到/opt/archive/jupyter/。
-
-
其他配置保持不变
-
其他配置项(如
autoindex、Content-Disposition等)保持不变,用于支持目录浏览和文件下载。
-
注意事项
-
检查路径权限
-
确保 Nginx 用户(通常是
www-data或nginx)有权限访问/opt/archive/jupyter/目录。
-
-
重新加载 Nginx 配置
-
修改配置后,运行以下命令检查配置文件语法是否正确:bash
sudo nginx -t -
如果配置文件没有问题,重新加载 Nginx 配置:bash
sudo nginx -s reload
-
-
测试访问
-
访问
http://127.0.0.1:31800/helloWeb/jupyter/,应该能够看到/opt/archive/jupyter/目录下的文件列表,并且可以下载文件。
-
通过这种方式,即使目录
/opt/archive/helloWeb/jupyter/ 不存在,也可以通过 alias 将其映射到实际的 /opt/archive/jupyter/ 目录。
浙公网安备 33010602011771号