2016-05-10 8 views
2

Linux上でNGINXサーバを設定しようとしていますが、ディレクトリからファイルをダウンロードしています。ディレクトリ内のファイルをダウンロードする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;

答えて

1

ファイルをブラウザに表示するのではなく、ダウンロードするには、MIMEタイプをapplication/octet-streamに設定する必要があります。とにかくデフォルトの型として宣言されることが多いのはです。

通常、nginxを使用するMIMEタイプを決定するために、ファイル拡張子を使用し、これは空集合でtypesディレクティブを指定することにより、場所にオフにすることができます。

URLに%エンコード文字を使用して、奇妙な文字のファイル名にアクセスできます。

autoindexを有効にして、indexをオフにしてファイルを参照したい場合があります。これにより、難しいファイル名もダウンロードできることがわかります。

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

注:私はそう名前に設定する以外に、indexをオフにするメカニズムを認識していないです。デフォルトのindex.htmlがダウンロードディレクトリの内容と衝突しない可能性があります。

this documentを参照してください。

関連する問題