首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用X-Accel-Redirect在Nginx-Django中下载文件

使用X-Accel-Redirect在Nginx-Django中下载文件
EN

Stack Overflow用户
提问于 2021-11-11 16:27:12
回答 1查看 126关注 0票数 0

我有一个运行在Nginx-Uwsgi上的Django网页。我想让用户下载超过10 to的文件。为此,我使用X-Accel-Redirect

这个想法是用户应该去http://XXX.XX.XX.XX/main/download,在那里它可以下载一个文件。

经过几次配置后,现在我在浏览器上得到了一个403HTTP错误,在Nginx上得到了以下跟踪

代码语言:javascript
复制
2021/11/11 16:11:45 [error] 29309#0: *1 open() "/home/myuser/direct/example.txt" failed (13: Permission denied), client: 2.136.173.243, server: _, request: "GET /main/download HTTP/1

我的Nginx配置如下:

代码语言:javascript
复制
events {
    worker_connections 10000;
}

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;

    client_max_body_size 128M;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    server_names_hash_bucket_size 256;

    # 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;

    upstream django {
        server 127.0.0.1:8000;
    }

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

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

        location /download {
            internal;
            alias /home/myuser/direct;
            proxy_max_temp_file_size 0;
        }



        location /main {
            uwsgi_pass django;
            uwsgi_param Host $host;
            uwsgi_param X-Real-IP $remote_addr;
            uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
            uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

            uwsgi_param QUERY_STRING $query_string;
            uwsgi_param REQUEST_METHOD $request_method;
            uwsgi_param CONTENT_TYPE $content_type;
            uwsgi_param CONTENT_LENGTH $content_length;
            uwsgi_param REQUEST_URI $request_uri;
            uwsgi_param PATH_INFO $document_uri;
            uwsgi_param DOCUMENT_ROOT $document_root;
            uwsgi_param SERVER_PROTOCOL $server_protocol;
            uwsgi_param HTTPS $https if_not_empty;
            uwsgi_param REMOTE_ADDR $remote_addr;
            uwsgi_param REMOTE_PORT $remote_port;
            uwsgi_param SERVER_PORT $server_port;
            uwsgi_param SERVER_NAME $server_name;
        }
   }

我调用/downloadDjango函数是:

代码语言:javascript
复制
def download(request):
    response = HttpResponse()
    path = "/home/myuser/direct/example.txt"
    name = "example.txt"
    #response['Content_Type']='application/octet-stream'
    #response["Content-Disposition"] = "attachment; filename={0}".format(
    #        name.encode('utf-8'))
    response['Content-Length'] = os.path.getsize(path)
    response['X-Accel-Redirect'] = "/download/{0}".format(name)
    del response['Content-Type']
    del response['Content-Disposition']
    del response['Accept-Ranges']
    del response['Set-Cookie']
    del response['Cache-Control']
    del response['Expires']
    return response

我还尝试将nginx配置中显示的用户更改为我的用户,以防访问/home时出现任何问题,但我得到了相同的错误。

因此,我的问题是,我如何解决这个错误,以便允许来自/home的Nginx服务文件可以下载?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-17 11:42:52

问题是nginx不希望从提供的目录中读取文件。当此目录更改为/var/wwww时,它可以按预期工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69931711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档