Flask App in Production Unable to Access Static Folder in NGINX Configuration

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật