Notification texts go here Contact Us Buy Now!

Expose static files from FastAPI app behind Traefik proxy

This comprehensive guide will help you expose static files from your FastAPI app behind a Traefik proxy. Our example setup includes Traefik and the API set up as services, with Traefik acting as a reverse proxy.

├── back
│   ├── Dockerfile
│   ├── main.py
│   ├── requirements.txt
│   └── static
│       └── test.html
└── docker-compose.yml

1. Configure Traefik

In the docker-compose.yml file, Traefik is configured with the following labels:

  • traefik.enable=true: Enables Traefik for this service.
  • traefik.http.routers.fhback.rule=Host(`example.com`) && PathPrefix(`/`): Specifies the rule for matching incoming requests to this service. In this case, it matches requests to the example.com hostname and the / path prefix.
  • traefik.http.services.fhback.loadbalancer.server.port=8000: Specifies the port on which the FastAPI app is listening inside the container.
  • traefik.http.middlewares.firehouse-compress.compress=true: Enables GZIP compression for responses from this service.
  • traefik.http.routers.fhback.middlewares=firehouse-compress,fhback-stripprefix: Specifies the middleware to be applied to requests to this service. In this case, we're using the firehouse-compress middleware to enable GZIP compression and the fhback-stripprefix middleware to remove the / prefix from the request path before passing it to the FastAPI app.
  • traefik.http.middlewares.testheader.headers.customresponseheaders.X-Test-Header=TraefikProxy: Adds a custom response header called X-Test-Header with the value TraefikProxy to responses from this service.
  • traefik.http.routers.fhback.middlewares=testheader: Specifies that the testheader middleware should be applied to requests to this service.

2. Configure FastAPI

In the main.py file, we mount the static directory as a static file endpoint at /static using app.mount('/static', StaticFiles(directory='static'), name='static').

3. Build and Run the Application

Run docker-compose up to build and run the application. Make sure you have Docker installed and running.

4. Test the Application

Use curl to send a request to the FastAPI app and verify that the static file is served correctly:

curl -v --resolve example.com:80:127.0.0.1 http://example.com/static/test.html

You should see the content of the test.html file in the response. Additionally, you can check the Traefik logs to confirm that the request was proxied through Traefik.

This guide provides a solid foundation for exposing static files from your FastAPI app behind a Traefik proxy. Feel free to adapt it to your specific requirements.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.