D
D
drowzeenico2019-12-18 19:48:53
WordPress
drowzeenico, 2019-12-18 19:48:53

Why is Nginx not uploading files?

Good day. It is not possible to upload files to the server, namely through nginx. The application scheme is banal: requests are received by nginx, and nodejs processes them (using npm multer). Actually, locally, without nginx, everything works fine, the files are loaded. As soon as nginx appears, the entire boot breaks. Virtual host config:

server {
  listen 80;

  server_name app.dev;

  access_log /home/nicota/code/app/build/access.log req_len;

  error_log /home/nicota/code/app/build/error.log debug;

  location = /upload/file {
    proxy_http_version 1.1;
    limit_except POST          { deny all; }

    client_body_temp_path      /tmp/;
    client_body_in_file_only   on;
    client_body_buffer_size    2M;
    client_max_body_size       100М;
    client_body_timeout         10s;

    proxy_pass_request_headers on;
    proxy_set_header           X-FILE $request_body_file;
    proxy_set_body             off;
    proxy_redirect             off;

    proxy_set_header           Connection "";
    proxy_set_header           Host $host;
    proxy_set_header           X-Real-IP $remote_addr;
    proxy_set_header           X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header           X-Forwarded-Proto $scheme;
    proxy_set_header           X-File-Name $request_body_file;
    proxy_set_header           X-NginX-Proxy        true;
    proxy_pass                 http://127.0.0.1:3000/upload/file;

    proxy_connect_timeout       600;
    proxy_send_timeout          600;
    proxy_read_timeout          600;
    send_timeout                600;
  }


  location / {
    proxy_pass http://127.0.0.1:3000;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

What is at the moment: nginx loads the file into the /tmp temporary folder and that's it.
This file doesn't go anywhere else. That is, in nodejs, the request body and other data are simply missing.
curl request example:
curl -v -F '[email protected]/path/to/fome/file.png' http://app.dev/upload/file/

gives:
* Trying 127.0.100.2...
* Connected to app.dev (127.0.100.2) port 80 (#0)
> POST /upload/file/ HTTP/1.1
> Host: app.dev
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 248498
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------7194e139a35c069f
> 
< HTTP/1.1 100 Continue # здесь кажется, что требуется продолжение, но все резко обрубается
< HTTP/1.1 400 Bad Request
< Server: nginx/1.10.3 (Ubuntu)
< Date: Wed, 18 Dec 2019 16:39:48 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 2
< Connection: keep-alive
< X-Powered-By: Express
< Access-Control-Allow-Origin: *
< ETag: W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"
* HTTP error before end of send, stop sending
< 
* Closing connection 0
{}%

What is in access.log for this request:
127.0.0.1 -[18/Dec/2019:19:39:48 +0300] "POST /upload/file/ HTTP/1.1" 400 "curl/7.47.0" "248718"(это размер тела запроса в байтах) "multipart/form-data; boundary=------------------------7194e139a35c069f"(это Content-Type) "/var/lib/nginx/body/0048769492"(это временной файл)

Nothing is written to error.log.
What the nodejs application sees (headers, req.headers):
{ connection: 'upgrade',
  host: 'app.dev',
  'content-length': '248498',
  'user-agent': 'curl/7.47.0',
  accept: '*/*',
  'content-type': 'multipart/form-data; boundary=------------------------823d2202257dc89a' }

The body of the request in the application (req.body), of course, is empty.
As I see it, only the first part of the request arrives, only the headers, without the body. But where did it go? Considering that it was successfully loaded into the temporary folder?
I spent all day looking for a solution. Came here hoping for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-12-18
@drowzeenico

And what did you expect by writing proxy_set_body off ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question