gooseNJuice
gooseNJuice

Reputation: 33

Why can't I upload a big file to nginx server?

I'm writing apk uploading functionality in my React App. Every time I try to upload an apk file bigger than 10 mb, I get back an error: 413 Request Entity Too Large. I've already used client_max_body_size 888M; directive. Can anybody explain me, what I do wrong?

Here is my nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/errors.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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/accesses.log  main;

    sendfile        on;

    keepalive_timeout  65;

server {

  listen 80;
  listen [::]:80;
  server_name up-app.unpluggedsystems.app;
 #   return 301 http://$server_name$request_uri;

  root /usr/share/nginx/html;
  index index.html;

client_max_body_size 888M;

  location /api/7/apps/search {
   proxy_pass http://ws75.aptoide.com/api/7/apps/search;
   proxy_set_header X-Forwarded-For $remote_addr;

  }
location ~ \.(apk)$ {
  proxy_pass https://pool.apk.aptoide.com;
  proxy_set_header X-Forwarded-For $remote_addr;
}

location /api {
  proxy_pass https://up-app.unpluggedsystems.app;
  proxy_set_header X-Forwarded-For $remote_addr;
}

}
}

And Dockerfile that I use (maybe something wrong here?):

# build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
COPY create-env-file.sh ./create-env-file.sh
RUN npm install
RUN npm install [email protected] -g
COPY . ./
RUN npm run build

# production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY fullchain.crt /etc/ssl/fullchain.crt
COPY unpluggedapp.key.pem /etc/ssl/unpluggedapp.key.pem
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

I FE Junior, and I so weak in such things. Maybe I put client_max_body_size in wrong place?

Upvotes: 0

Views: 2659

Answers (1)

Dylan B
Dylan B

Reputation: 1010

Please move client_max_body_size under http{} section

http {
   # some code here
   sendfile on;
   client_max_body_size 888M;
   #...
}

Make sure to restart nginx After modifying the configuration file.

You can try

client_max_body_size 0;

(set to no limit, but this is not recommended for production env.)

More over, Remember that if you have SSL, that will require you to set the above limitation for the SSL server and location{} in nginx.conf too. If your client (browser) tries to upload on http, and you expect them to get 301'd to https, nginx server will actually drop the connection before the redirect due to the file being too large for the http server, so the limitation has to be set in both http and https.

The trick is to put in both http{} and server{} (for vhost)

http {
    # some code
    client_max_body_size 0;
}  



server {
    # some code
    client_max_body_size 0;
}

Upvotes: 1

Related Questions