0

In my server block I have

   location / {
      proxy_pass http://echocdn;
      include /etc/nginx/mime.types;
      add_header Cache-Control "max-age=31536000, private, no-transform, no-cache";
      location ~* \.apk$ {
         add_header Content-Type application/vnd.android.package-archive;
         add_header Content-Disposition "attachment";
      }
   }

If I remove the inner location block, I can reach the APK file (and all other files).

With the block added, any .apk file returns a 404. How do I add the headers for APK files?

Note: Content types for other file types are handled well by the included mime.types, but even if I add the line for APK files as described How to download ".apk" as ".apk"? (not as ".zip") and https://blog.mypapit.net/2015/08/how-to-set-apk-mime-type-for-nginx-web-server.html it only returns a content-type of text/html for apk files.

The Tahaan
  • 6,915
  • 4
  • 34
  • 54

3 Answers3

2

maybe it's better not to set a header directly, for me it resulted in a double content-type.

but if you declare it in the location-block types it also works fine.

take a look:

  location ~* \.apk$ {
    types {
      application/vnd.android.package-archive apk;
    }

    add_header Content-Disposition "attachment";
  }
stephanfriedrich
  • 563
  • 5
  • 20
0

You need to have proxy_pass inside the apk block also. Nginx doesn't work like a programming language so you are not executing anything from the parent block.

Also for setting the content type using default_type directive

   location / {
      proxy_pass http://echocdn;
      include /etc/nginx/mime.types;
      #proxy_pass http://127.0.0.1:8082;
      add_header Cache-Control "max-age=31536000, private, no-transform, no-cache";
      location ~* \.apk$ {
         default_type application/vnd.android.package-archive;
         add_header Content-Type application/vnd.android.package-archive;
         add_header Content-Disposition "attachment";
         proxy_pass http://echocdn;
      }
   }

You can even handle this with a if

   location / {
      proxy_pass http://echocdn;
      include /etc/nginx/mime.types;
      #proxy_pass http://127.0.0.1:8082;
      add_header Cache-Control "max-age=31536000, private, no-transform, no-cache";
      if ($request_uri ~* \.apk$) {
         default_type application/vnd.android.package-archive;
         add_header Content-Type application/vnd.android.package-archive;
         add_header Content-Disposition "attachment";
      }
   }
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Adding the proxy_pass directive inside the inner block gets rid of the 404 - the file is now found, but the Content-Type is still text/html. – The Tahaan Sep 18 '17 at 18:43
  • The if-directive option would not start until I removed the default_type directive (The log said it cannot be used there). Without it, it acts the same as the other option - the file is received but the content-type is not set. – The Tahaan Sep 18 '17 at 18:47
0

Try this:

location ~*.(apk)${
    root /tmp/app; 
    add_header Content-Disposition attachment;
}

/tmp/app is your APK files full path.

dinasind
  • 31
  • 2