【问题标题】:How to configure NGINX server which download any files in derectory如何配置下载目录中任何文件的 NGINX 服务器
【发布时间】:2016-09-05 00:15:38
【问题描述】:

我正在尝试在 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;

【问题讨论】:

    标签: linux nginx centos nginx-location winginx


    【解决方案1】:

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

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

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

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

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

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

    请参阅this document 了解更多信息。

    【讨论】:

    • 什么是 not_a_file
    • @dieter 请参阅我答案末尾的斜体注释。
    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多