envoyproxy / envoy

Cloud-native high-performance edge/middle/service proxy
https://www.envoyproxy.io
Apache License 2.0
24.94k stars 4.8k forks source link

Ability to serve static files #378

Open garrettheel opened 7 years ago

garrettheel commented 7 years ago

It would be great to have the ability to serve static files from Envoy.

My use-case is to serve static files for an HTML frontend (HTML, JS, CSS, etc.), which should be a fairly common one. Right now you would have to put something like nginx in the middle to efficiently do this.

rshriram commented 7 years ago

@garrettheel would this end up turning Envoy into full fledged web server? Because, we would now have to take care of file encodings, gzip stuff, cache headers, and so on. Just afraid of code complexity/bloat.

mattklein123 commented 7 years ago

This would only ever be added as a standalone filter, but overall I agree with @rshriram that we are unlikely to add this into the core server. But will leave open just for tracking purposes.

bgaff commented 7 years ago

Why not add fastcgi as a filter? Seems like a win-win.

skippy commented 7 years ago

@mattklein123, what I was hoping for was a way to serve static files on certain non-2xx responses, such as a 500 or 403. So rather than returning the envoy default msg, it could return a file.

something along the lines of haproxy's errorfile 403 /etc/haproxy/403.json HAProxy loads these up in memory at startup (at least they did in 1.6).

is something like that doable?

[edit]: add haproxy note

mattklein123 commented 7 years ago

@skippy it's definitely doable and I'm not opposed, but someone would need to do a proper design and implement.

skippy commented 7 years ago

@mattklein123 if you are ok with it, I'll make a separate issue to design and track static files for error handling... that is a much more specific use-case than general static files (I agree with your comments on that score).

but in the meanwhile, time to dust off haproxy so I can serve error files :)

mattklein123 commented 7 years ago

if you are ok with it, I'll make a separate issue to design and track static files for error handling

sure sounds good

weilip1803 commented 5 years ago

Is this still being done? If it isnt is there an example of how I can serve static files from an upstream?

htuch commented 5 years ago

@weilip1803 you can do basic file serving with https://www.envoyproxy.io/docs/envoy/latest/api-v2/api/v2/route/route.proto#route-directresponseaction. This is not a full fledged web server. I think this thread captures current status, which is "help wanted".

GNUGradyn commented 2 years ago

I found this from an (older?) Envoy docs page. Is this module still maintained? https://jteso.github.io/envoy/documentation/site/modules/static/

ghevge commented 2 years ago

Was this functionality implemented or it is still work in progress ? Thanks!

YvesZHI commented 1 year ago

So has Envoy supported this function?

KBaichoo commented 1 year ago

There's an additional feature in the work to provide custom error response based on if the upstream request errored: https://github.com/envoyproxy/envoy/issues/21441

Spongman commented 1 year ago

investigating nginx alternatives. Envoy looks promising but really surprised to find it can't serve static content. shame.

EDIT: not sure why people find this confusing. am i missing something?

Jiehong commented 1 year ago

@Spongman give a try to caddy instead

ningyougang commented 1 year ago

@Spongman

I used below configuration to get local static page directly

# cat envoy-demo-static.yaml
static_resources:

  listeners:
  - name: listener_0
    address:
      socket_address:
        address: 0.0.0.0
        port_value: 10000
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          access_log:
          - name: envoy.access_loggers.stdout
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
          http_filters:
          - name: envoy.filters.http.router
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                direct_response:
                  status: 200
                  body:
                    filename: "/etc/envoy/hello-envoy.html"

And start a container with above configuration

docker run -v /tmp/envoy-demo-static.yaml:/etc/envoy/envoy.yaml \
-v /tmp/hello-envoy.html:/etc/envoy/hello-envoy.html \
-d  --name envoy-static-container -p 9901:9901 -p 10000:10000 \
envoyproxy/envoy:v1.27-latest  -c /etc/envoy/envoy.yaml

(hello-envoy.html is a very simple static file)

m44soroush commented 1 year ago

You can also add Content-Type to response header with value text/html to be parsed as an html file in your browser.

sample config:

...
route_config:
  name: local_name: local_route
  virtual_hosts:
  - name: local_service
    domains: ["*"]
    routes:
    - match:
        prefix: "/"
      direct_response:
        status: 200
        body:
          filename: "/etc/envoy/hello-envoy.html"
    response_headers_to_add:
        - header:
              key: "Content-Type"
              value: "text/html"
...
hakan-77 commented 2 months ago

We use Istio/envoy for a quite complex K8s system without any need for Nginx. Except, to serve static files. Would be really nice to have one gateway/reverse proxy to rule them all.