I have a spring boot app that uploads a file to a filepath which works in localhost on port 5000. On AWS environment, app jar is bundled with nginx configuration for reverse proxy, which is working fine except for a page with form with enctype="multipart/form-data"
. It fails with 403 Forbidden
with out any logs on the nginx access/error logs, request doesn’t reach Tomcat as well. Files of a 1KB size itself fails, I even removed saving to disk/S3, as below.
Controller
@GetMapping("/inputuploads")
public String listInputUploads(Model model) {
logger.info("Renedering upload page");
return "inputuploads";
}
@PostMapping("/inputuploads")
public String saveFile(Model model, @RequestParam("file") MultipartFile file, RedirectAttributes ra) throws IOException {
logger.info("Saving file:"+file.getOriginalFilename());
ra.addFlashAttribute("message","Uploaded file: "+ file.getOriginalFilename());
return "redirect:/inputuploads";
}
template
<!DOCTYPE html>
<html lang="en" xmsns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Input Upload</title>
</head>
<body>
<div th:if="${message}">
<div th:text="${message}">
</div>
</div>
<div >
<form method="post" th:action="@{/inputuploads}" enctype="multipart/form-data" >
<div>
<input type="file" name="file" accept=".xlsm" >
</div>
<div>
<button type="submit" >Upload</button>
</div>
</form>
</div>
</div>
</section>
</body>
</html>
.platform/nginx/nginx.conf
user nginx;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 32633;
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"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl default_server;
# Note : il faut utiliser un chemin relatif ici (car la config nginx est testee avant d etre deplacee)
ssl_certificate certificates/localhost.crt;
ssl_certificate_key certificates/localhost.key;
#ssl_certificate certificates/public.crt;
#ssl_certificate_key certificates/csr.pem;
#ssl_certificate_key certificates/privatekey.pem;
access_log /var/log/nginx/access.log main;
client_header_timeout 60;
client_body_timeout 60;
keepalive_timeout 60;
gzip off;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Note : la configuration du fw vers le tomcat ainsi que la redirection se font dans un autre fichier
include conf.d/elasticbeanstalk/*.conf;
}
}
.platform/nginx/conf.d/client_max_body_size.conf
client_max_body_size 100M;
.platform/nginx/conf.d/elasticbeanstalk/00_application.conf
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
.ebextensions/00-set-timezone.config
commands:
set_time_zone:
command: ln -f -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime