linux - 如何配置 NGINX 服务器下载目录中的任何文件

标签 linux nginx centos nginx-location winginx

我正在尝试在 Linux 上配置 NGINX 服务器,该服务器从目录下载任何文件。

但我面临的问题是,当文件是文本文件或文件名包含任何特殊字符(如空格和(“()”#“&”)时,浏览器将不会接受并且不会给我任何内容。

如何解决这个问题? 我的配置如下

http {
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;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 2048;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;
#default_type        application/*;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

index   index.html index.htm;

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {}

error_page 404/404.html; error_page 500 502 503 504/50x.html;

最佳答案

要下载文件而不是在浏览器中显示它们,MIME 类型应设置为 application/octet-stream无论如何,它通常被声明为默认类型。

通常,nginx 使用文件扩展名来确定要使用的 MIME 类型,但可以通过使用空集指定 types 指令来在某个位置关闭此功能.

可以使用 URL 中的 % 编码字符来访问包含奇怪字符的文件名。

作为实验,您可能需要打开 autoindex 并关闭 index 来浏览文件。这也将告诉您,您的困难文件名也可以下载。

location / {
    index not_a_file;
    autoindex on;
    types {}
}

注意:除了将其设置为一个不太可能的名称之外,我不知道关闭 index 的机制。默认的 index.html 可能不会与您的下载目录的内容冲突。

参见this document了解更多。

关于linux - 如何配置 NGINX 服务器下载目录中的任何文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37131398/

相关文章:

c - 每次从一个目录更改到另一个目录并从那里调用 make 时,目标文件都会被编译

linux - 我想使用 Basic authtype 处理 CUPS 身份验证,如何自定义 PAM?

linux - 如何更改 Apache 中包的路径?

ruby-on-rails - Rails 忽略 config.action_dispatch.x_sendfile_header?使用 Thin + Nginx

makefile - 如何修复我的makefile?

linux - CMOS_WRITE 无法工作的可能原因是什么?

nginx - 将 Integrity CI 部署到 Nginx 和 Unicorn 上的子目录

django - 错误 apache 配置错误 : couldn't perform authentication. AuthType 未设置!:/在 centos 6

python - 在 Linux 中构建可移植的 Tesseract OCR 库