title: Download file in Nginx-Django with X-Accel-Redirect
body: <p>I have a Django webpage running on Nginx-Uwsgi. I want to let the user download files of over 10GB. For this purpose, I am using <code>X-Accel-Redirect</code>.</p>
  <p>The idea is that the user should go to <code>http://XXX.XX.XX.XX/main/download</code> and there it can download a file.</p>
  <p>After several configurations, now I get a 403 HTTP error on the browser and the following trace on <code>Nginx</code>:</p>
  <pre><code>2021/11/11 16:11:45 [error] 29309#0: *1 open() &quot;/home/myuser/direct/example.txt&quot; failed (13: Permission denied), client: 2.136.173.243, server: _, request: &quot;GET /main/download HTTP/1
  </code></pre>
  <p>My Nginx configuration is as follows:</p>
  <pre><code>events {
      worker_connections 10000;
  }
  
  http {
      log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                        '$status $body_bytes_sent &quot;$http_referer&quot; '
                        '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';
  
      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;
          }
     }
  </code></pre>
  <p>My <code>Django</code> function that calls <code>/download</code> is:</p>
  <pre><code>def download(request):
      response = HttpResponse()
      path = &quot;/home/myuser/direct/example.txt&quot;
      name = &quot;example.txt&quot;
      #response['Content_Type']='application/octet-stream'
      #response[&quot;Content-Disposition&quot;] = &quot;attachment; filename={0}&quot;.format(
      #        name.encode('utf-8'))
      response['Content-Length'] = os.path.getsize(path)
      response['X-Accel-Redirect'] = &quot;/download/{0}&quot;.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
  </code></pre>
  <p>I have also tried changing the user that appears on the nginx configuration to my user in case there were any trouble accessing /home, but I got the same error.</p>
  <p>Therefore, my question is, how can I solve this error in order to allow Nginx serving files from /home that could be downloaded?</p>