413 Request Entity Too Large问题解决方法

最近我们有个小程序需求是现场拍照提取照片中的有效信息,上传图片只能通过现场拍照,由于目前手机像素普遍较高,导致上传接口出现413 Request Entity Too Large,上传文件过大引起nginx代理报错。

针对这个问题,解决方案是:

  1. 项目配置文件修改,比如springboot项目中的application文件添加或修改以下参数;

http:
    multipart:
      max-file-size: 200Mb
      max-request-size: 200Mb
  1. 修改nginx配置

打开nginx主配置文件nginx.conf, 找到http{}段、server段、location段(上传文件代理的服务器)并修改或添加以下内容:client_max_body_size 200m;

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    server_names_hash_bucket_size 64; 
    
    include       mime.types;
    sendfile        on;
    keepalive_timeout  200;
    client_header_timeout 120s;
    client_body_timeout 120s;
    client_max_body_size 200m;
    gzip  on;
    
    server {
    listen       xxx xxx;
    server_name  xxxxxxx;
    ssl_certificate xxxxx;
    ssl_certificate_key xxxxxxx;

    ssl_session_timeout 5m;
    ssl_ciphers xxxxxxxxx;
    ssl_protocols xxxxxx;
    ssl_prefer_server_ciphers on;
    client_max_body_size 200m;

    location /iis/ {
        proxy_pass http://localhost:xxxx/;
        proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
        
    location /tomcat/ {
        proxy_pass http://localhost:xxxx/;
        proxy_connect_timeout 600;
            proxy_read_timeout 600;
        client_max_body_size 20m;
        }
        
    location /zcsd/ {
        proxy_pass xxxxxxxxxxxxxx;
        proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    
    error_page   500 502 503 504  /xx.html;
        location = /xx.html {
            root   html;
        }
    }
}
  1. 打开Tomcat中server.xml文件,在以下位置最后一行添加maxPostSize="200"

<Connector port="8086" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               maxPostSize="200"/>

猜你喜欢

转载自blog.csdn.net/weixin_40205234/article/details/128930331