2
votes

I am trying to configure NGINX server on Linux which downloads any files from the directory.

But the problem I am facing is when the file is a text file or the file name contains any special character like spaces and ( "()"#"&" ) then the browser will not entertain and gives me nothing.

How do I solve this issue? my Configuration is as fallows

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;

1

1 Answers

1
votes

To download files rather than display them in the browser, the MIME type should be set to application/octet-stream. Which is often declared as the default type anyway.

Normally, nginx uses the file extension to determine which MIME type to use, but this can be turned off in a location by specifying the types directive with an empty set.

Filenames with strange characters can be accessed using %-encoded characters in the URL.

As an experiment, you might want to turn on autoindex and turn off index to browse the files. This will also show you that your difficult filenames can be downloaded too.

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

Note: I am not aware of a mechanism to turn off index, other than setting it to an unlikely name. It is possible that the default index.html may not collide with the contents of your download directories.

See this document for more.