I was hoping you could help me with a problem I’m facing. I’m running a Flask app with NGINX in production, and the issue is that when I try to upload an image, it says that the folder src/static/new/news_images cannot be found. The path seems correct, and I also tried using an absolute path, but it didn’t work either.
I’m using the following NGINX configuration, but it’s not resolving the issue:
events {
worker_connections 1024;
}
http {
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server {
listen 80 default_server;
listen 443 ssl;
ssl_certificate /etc/ssl/certs/example.com.cer;
ssl_certificate_key /etc/ssl/private/example.com.pem;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Configuration to serve static files
location /static/ {
alias /home/user/project/src/static/;
}
}
}
and this its the error:
[2024-09-09 16:19:15,383] ERROR in app: Exception on /upload_news/Subir_examen [POST]
Traceback (most recent call last):
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/flask/app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/flask/app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/flask/app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/flask/app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/flask_login/utils.py", line 290, in decorated_view
return current_app.ensure_sync(func)(*args, **kwargs)
File "/home/diego/Escritorio/fuerzag/src/app.py", line 231, in upload_news
image_exam.save(image_exam_path)
File "/home/diego/Escritorio/fuerzag/venv/lib/python3.10/site-packages/werkzeug/datastructures/file_storage.py", line 125, in save
dst = open(dst, "wb")
FileNotFoundError: [Errno 2] No such file or directory: 'src/static/img/img_exams/modelo_numero_3.jpg'
[2024-09-09 16:19:41 -0300] [4970] [INFO] Handling signal: winch
Has anyone experienced the same issue? I understand that uploading images to a folder in the app is not the best practice (I should probably use a cloud service for storage), but for now, I’m testing this approach.
Any help would be greatly appreciated! If you need more information, feel free to ask.
Thanks in advance!
I’ve been trying to solve an issue with my Flask app running on Nginx. The app is in production, but when I try to upload an image, I get an error saying that the folder src/static/new/news_images can’t be found. I’ve double-checked the path and even tried using an absolute path, but it still doesn’t work.
I’ve tried adding a route to serve static files both in the global Nginx configuration and in the default app configuration, but neither has worked. I understand that many people might suggest using a cloud service to store images, and I plan to explore that eventually. However, for now, I’d like to test uploading images directly from within the app as a starting point.
If anyone has encountered a similar problem or has suggestions, I’d really appreciate it. Thanks in advance! Let me know if you need any more information.