为什么我在 Nginx 中看到错误:413“请求实体太大”?

问题描述 投票:0回答:1

我在下载大图像(大约超过 2 megabats)时遇到问题。

POST https://apis.mysite.com/apis/auth/ads net::ERR_FAILED 413 (Payload Too Large)

我不知道问题是什么。

在 Nginx 设置中我指定了

client_max_body_size 100M
;

    worker_processes  1;

    events {
        worker_connections  1024;
    }



    http {



        include       mime.types;
        default_type  application/octet-stream;

        sendfile        on;
        keepalive_timeout  65;


        ssl_certificate ./ssl/mysite.crt;
        ssl_certificate_key ./ssl/mysite.key;


        client_max_body_size 100M;

        upstream apispring {

                server 127.0.0.1:8080;
            }




         server {

            listen              443 ssl;
            server_name  apis.mysite.com;  

            location / {
            proxy_pass http://apispring;


             }

         }



         server {


            listen              443 ssl;
            server_name         mysite.com www.mysite.com;       

            location / {
                root   html;
                try_files $uri $uri$args $uri$args/ /index.html;
            }

        }



    }

我还指定了 Spring Boot 设置

    server.port = 8080

    server.tomcat.threads.max = 200
    server.tomcat.connection-timeout = 5s
    server.tomcat.max-swallow-size = 2MB
    server.tomcat.max-http-form-post-size = 50MB

    server.max-http-request-header-size = 8KB

    spring.servlet.multipart.max-file-size = 50MB
    spring.servlet.multipart.max-request-size = 50MB

我也使用 Angular

    previewImages: Set<string> = new Set();



    submit() {



        const ads: any = {
          phone: '1111111111',
          email: [email protected],
        }



        const formData = new FormData();
        formData.append('ads', JSON.stringify(ads))

        let num = this.previewImages.size;
        let count: number = 0

        for (const [value] of this.previewImages.entries()) {
          formData.append(`img${count}`, value)
          count = count + 1
          if (count === num) {
            count = 0
          }

        }


        this.httpRS.addAds(formData).subscribe({
          next: () => {
            //
          }

        });


    }

还有我的 Spring Boot 控制器类

    @RestController
    @RequestMapping("/apis/auth")
    public class Ads {




        @PostMapping(value = "/ads", produces = "application/json")
        public viod addAds(@RequestParam(value = "img0", required = false) String img1,
                           @RequestParam(value = "img1", required = false) String img2,
                           @RequestParam(value = "ads") String ads) {

            // body

        }


    }

请告诉我为什么会出现这个错误?

java angular spring-boot nginx file-upload
1个回答
0
投票

您收到此错误是因为如果您正在上传文件,您的请求数据不是

"multipart/form-data"
内容类型。要处理此类请求,您需要在您希望接收文件的位置添加
MaltipartFile
类型的参数。

@RequestParam("file") MultipartFile[] files)

使用相同的名称命名每个

file
,以便在控制器方法中轻松使用。


如果您有更多问题,那么您应该参考原始的docs来开始。


为什么我在 Nginx 中看到错误:413“请求实体太大”?

发生这种情况是因为您的 nginx 服务器不允许您上传大于 nginx 配置文件中定义的大小的文件。

要解决这个问题,你必须修改你的nginx配置。

按照以下步骤添加/修改您的 nginx 配置文件:

  1. 使用终端连接到您的 nginx 服务器。
    ssh user@my_server
  2. 转到配置位置并打开它
    sudo vim /etc/nginx/nginx.conf
  3. 添加/修改
    client_max_body_size
    变量。 如果找到了,就把它的大小增加到100M, 如果不存在,则可以在 HTTP 设置内部和末尾添加以下代码:
    client_max_body_size 100M;
  4. 重新启动 nginx 以应用更改
    sudo service nginx restart
© www.soinside.com 2019 - 2024. All rights reserved.