1. 上传图片大小限制
背景:使用Java做文件上传时,大小限制为10M,但是超过 1M的文件却上传失败,前台报 413异常
问题:程序通过 nginx进行了转发,nginx默认的客户端body大小为 1M(查看nginx的错误日志,可以找到client intended to send too large body: 4134591 bytes)
解决:可以在nginx的配置文件中,在http、server、location等配置块内,添加 client_max_body_size
来调整客户端允许的最大上传 body大小(设置为0, 表示不限制)
http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# ================================= 在此处进行配置 =============================
client_max_body_size 10m;
# .....
}
2. 通过 nginx反向代理 MySQL
在nginx.conf 配置文件中,http配置块的外面
http {
# ....
}
stream {
upstream mysql {
server 10.166.1.1:3306;
}
server {
listen 9001 so_keepalive=on;
proxy_pass mysql;
proxy_timeout 24h;
}
}
3. 反向代理Redis
与反向代理MySQL的配置相同
http {
# ....
}
stream {
upstream redis {
server 127.0.0.1:6379;
}
server {
listen 9001;
proxy_pass redis;
proxy_timeout 3s;
proxy_connect_timeout 1s;
}
}