shahryarjb

shahryarjb

NGINX doesn't let phoenix to send file to it's user

hello, I create these post before and made my code in localhost that works for me perfect :sweat_smile: ,

https://forum.elixirforum.com/t/downloading-with-user-token/

https://forum.elixirforum.com/t/how-can-i-send-a-chunked-file-to-a-user-for-download


but today I decided to move my project into my server, the server uses NGINX and docker but doesn’t let me download with Phoenix, Please see my code!!

my code:

  def download(conn, file_path, file_type, file_name, "normal") do
    file = File.read!("#{file_path}")

    conn
     |> put_resp_content_type("#{file_type}")
     |> put_resp_header("Content-disposition","attachment; filename=\"#{file_name}#{Path.extname(file_path)}\"")
     |> put_resp_header("X-Accel-Redirect", "/tempfile/download/#{file_name}#{Path.extname(file_path)}")
     |> put_resp_header("Content-Type", "application/octet-stream")
     |> send_resp(200, file)
  end

I have tested 2 file format and size, the lowest size and big file, but they were different.

when I tested big file I had this error just in my safari that my browser download html file and tells me

no route found for GET /lms/download/c416ed0c-67d3-455c-b6f3-77273228be22.mp4 (KhatoghalamWeb.Router)

but the low size shows me a nginix default error

404 Not Found

nginx

you can see these link online: http://bit.ly/31yFISx

Middle of the page you can see this image and click on download icon

chrome error:

This site can’t be reached The web page at https://newkhat.khatoghalam.com/lms/download?chapter_id=5e638cce-e6b0-4d14-89f8-1f754c191ea9&type=رایگان might be temporarily down or it may have moved permanently to a new web address.
ERR_INVALID_RESPONSE

how can I fix this ?


Update

Big file download like video iex console on server:

[info] GET /lms/download
[debug] Processing with KhatoghalamWeb.ClientLmsController.download/2
  Parameters: %{"chapter_id" => "5e638cce-e6b0-4d14-89f8-1f754c191ea9", "type" => "رایگان"}
  Pipelines: [:browser, :csrf]
[debug] QUERY OK source="lms_chapters" db=1.9ms queue=0.3ms
SELECT l0."id", l1."id", l1."course_id", l0."download_link", l0."inserted_at", l0."updated_at" FROM "lms_chapters" AS l0 INNER JOIN "lms_headlines" AS l1 ON l1."id" = l0."headline_id" WHERE (l0."id" = $1) AND (((l0."status" = TRUE) AND (l1."status" = TRUE)) AND (l0."type" = 'رایگان')) [<<94, 99, 140, 206, 230, 176, 77, 20, 137, 248, 31, 117, 76, 25, 30, 169>>]
[debug] QUERY OK source="lms_courses" db=2.1ms queue=0.1ms
SELECT l0."id" FROM "lms_courses" AS l0 INNER JOIN "lms_categories" AS l1 ON l1."id" = l0."category_id" WHERE (l0."id" = $1) AND ((l0."status" = TRUE) AND (l1."status" = TRUE)) [<<57, 222, 97, 78, 28, 189, 70, 214, 160, 161, 15, 29, 94, 185, 131, 112>>]

[info] Sent 200 in 9ms
[info] GET /tempfile/download/5e638cce-e6b0-4d14-89f8-1f754c191ea9.mp4
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /tempfile/download/5e638cce-e6b0-4d14-89f8-1f754c191ea9.mp4 (KhatoghalamWeb.Router)
    (khatoghalam) lib/phoenix/router.ex:316: KhatoghalamWeb.Router.call/2
    (khatoghalam) lib/khatoghalam_web/endpoint.ex:1: KhatoghalamWeb.Endpoint.plug_builder_call/2
    (khatoghalam) lib/plug/debugger.ex:122: KhatoghalamWeb.Endpoint."call (overridable 3)"/2
    (khatoghalam) lib/khatoghalam_web/endpoint.ex:1: KhatoghalamWeb.Endpoint.call/2
    (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
    (cowboy) /khatogh/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
    (cowboy) /khatogh/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
    (cowboy) /khatogh/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

low file size in server iex console

[info] GET /lms/download
[debug] Processing with KhatoghalamWeb.ClientLmsController.download/2
  Parameters: %{"chapter_id" => "c416ed0c-67d3-455c-b6f3-77273228be22", "type" => "رایگان"}
  Pipelines: [:browser, :csrf]
[debug] QUERY OK source="lms_chapters" db=2.0ms queue=0.7ms
SELECT l0."id", l1."id", l1."course_id", l0."download_link", l0."inserted_at", l0."updated_at" FROM "lms_chapters" AS l0 INNER JOIN "lms_headlines" AS l1 ON l1."id" = l0."headline_id" WHERE (l0."id" = $1) AND (((l0."status" = TRUE) AND (l1."status" = TRUE)) AND (l0."type" = 'رایگان')) [<<196, 22, 237, 12, 103, 211, 69, 92, 182, 243, 119, 39, 50, 40, 190, 34>>]
[debug] QUERY OK source="lms_courses" db=1.2ms queue=0.9ms
SELECT l0."id" FROM "lms_courses" AS l0 INNER JOIN "lms_categories" AS l1 ON l1."id" = l0."category_id" WHERE (l0."id" = $1) AND ((l0."status" = TRUE) AND (l1."status" = TRUE)) [<<57, 222, 97, 78, 28, 189, 70, 214, 160, 161, 15, 29, 94, 185, 131, 112>>]

Marked As Solved

OvermindDL1

OvermindDL1

You aren’t sending the file, you are sending the contents, your code should be like:

  def download(conn, file_path, file_type, file_name, "normal") do
    conn
     |> put_resp_content_type("#{file_type}")
     |> put_resp_header("Content-disposition","attachment; filename=\"#{file_name}#{Path.extname(file_path)}\"")
     |> put_resp_header("Content-Type", "application/octet-stream")
     |> send_file(200, "#{file_path}")
  end

It takes a filename, it can’t stream an existing body. send_file/5 works by asking the kernel to send the file contents to the socket, it never enters userspace at all, no faster method of sending a file (in general, in reality it chunks it in certain modes, streams it in others, etc… it tries to do whatever is fastest and most efficient without overwhelming things).

Also Liked

OvermindDL1

OvermindDL1

You might be sending too much data in one burst since you say it only doesn’t work on “big files”.

This is loading the entire big file into active memory, yet you aren’t doing anything to it, unsure why?

Then you are sending the file out in one big message instead of streaming it out.

You should be using send_file/5 instead, this will stream it out, not eating RAM, and will naturally throttle to how fast the proxy will send it. Or you can chunk it manually but that’s a lot of work for no gain. ^.^;

If you switch to that though, does it work now?

NobbZ

NobbZ

How does your routes look like?

NobbZ

NobbZ

What’s your route for the /tempfile/...? You are redirecting there and therefore that’s what the client will request eventually.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement