[英]How to upload an image using NodeJS, Nginx and base64? (413 PayloadTooLargeError: request entity too large)
I am trying to upload 5 base64 images using Postman raw text option, with application/json header set. 我正在尝试使用Postman原始文本选项上传5张base64图像,并设置了application / json标头。 The server keeps returning the error mentioned.
服务器不断返回所提到的错误。 I am using NodeJS behind Nginx reverse proxy.
我在Nginx反向代理后面使用NodeJS。
I have tried all of the options mentioned in the code below, including increasing the limits up to (100M), which apparently wasn't the issue. 我尝试了以下代码中提到的所有选项,包括将限制增加到(100M),这显然不是问题。
NodeJS (MAXIMUM_FILE_SIZE_LIMIT='500mb'): NodeJS(MAXIMUM_FILE_SIZE_LIMIT ='500mb'):
app.use(bodyParser.urlencoded({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, parameterLimit: 100000, extended: true }));
app.use(bodyParser.json({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, extended: true }));
// app.use(bodyParser.text({ limit: process.env.MAXIMUM_FILE_SIZE_LIMIT, extended:true }));
app.use( bodyParser.text({type : 'application/text-enriched', limit: process.env.MAXIMUM_FILE_SIZE_LIMIT}) );
app.use( bodyParser.raw({limit: process.env.MAXIMUM_FILE_SIZE_LIMIT}) );
Nginx Nginx的
Configuration inside (server, http, location): 内部配置(服务器,http,位置):
nginx.conf, nginx.conf,
sites-enabled/default, 网站启用/默认,
sites-available/default: 网站可用/默认:
client_max_body_size 100M
I want to succeed uploading the images (5), which their total size is not more than 2MB. 我想成功上传图像(5),其总大小不超过2MB。
The solution finally was to leave the nginx's client_max_body_size 5M;
最终的解决方案是将Nginx的
client_max_body_size 5M;
保留为client_max_body_size 5M;
only at the nginx.conf
, as-well as using the following lines in my NodeJS code: 仅在
nginx.conf
,以及在NodeJS代码中使用以下几行:
app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
app.use(bodyParser.json({ limit: '5mb' }));
PLEASE NOTE!!! 请注意!!!
After trying a lot of combinations the problem appeared to be unrelated to the metnioned above, at all. 尝试了多种组合后,该问题似乎与上面提到的问题无关。
While using the node
command in-order to execute the server, everything worked great. 在按顺序使用
node
命令执行服务器时,一切工作正常。 But, because I wanted to keep the application alive (in-case of a server reboot), I have used forever
. 但是,因为我想保持应用程序正常工作(套内的服务器重启),我用
forever
。
Forever started the application, but apparently kept throwing the 413
error everytime, regardless of nginx's and node's settings. Forever启动了该应用程序,但显然每次都始终抛出
413
错误,无论nginx和节点的设置如何。
Switched to PM2
, which finally solved that. 切换到
PM2
,最终解决了该问题。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.