V2: Server Sent Events from Flask to Caddy via Gunicorn

1. My Caddy version (caddy version):

v2.0.0-rc.3

2. How I run Caddy:

caddy start

a. System environment:

Ubuntu 18.04LTS

b. Command:

gunicorn --bind 0.0.0.0:5000 wsgi:app --daemon

d. My complete Caddyfile or JSON config:

demo.domain.com {
    root * /var/www/Project
    tls email@domain.com
    reverse_proxy localhost:5000 {
        header_up Host {http.request.host}
        header_up X-Real-IP {http.request.remote.host}
        header_up X-Forwarded-For {http.request.remote.host}
        header_up X-Forwarded-Port {http.request.port}
        header_up X-Forwarded-Proto {http.request.scheme}
    }
    header {
        X-Content-Type-Options nosnif
        X-Frame-Options DENY
        Referrer-Policy no-referrer-when-downgrade
   }
}

3. The problem I’m having:

I have developed a flask application that relies on Server Sent Events. The application works fine with gunicorn if I connect to the application directly and via flask as well but connecting via Caddy results in no SSE events being sent and no communication made to the /stream endpoint. I’ve seen in v1 of Caddy there was a suggestion to disable gzip. Not sure if I’m doing anything wrong with my configuration file.

4. Error messages and/or full log output:

127.0.0.1 - - [03/May/2020 00:59:30] “GET /stream/1 HTTP/1.1” 200 -

root@VirtQueue:/var/www/VirtQueue# caddy run
2020/05/03 01:25:18.099 INFO using adjacent Caddyfile
2020/05/03 01:25:18.101 INFO admin admin endpoint started {“address”: “tcp/localhost:2019”, “enforce_origin”: false, “origins”: [“localhost:2019”, “[::1]:2019”, “127.0.0.1:2019”]}
2020/05/03 01:25:18.101 INFO http enabling automatic HTTP->HTTPS redirects {“server_name”: “srv0”}
2020/05/03 01:25:18.102 INFO tls cleaned up storage units
2020/05/03 01:25:18.102 INFO http enabling automatic TLS certificate management {“domains”: [“domain.com”]}
2020/05/03 01:25:18.114 INFO autosaved config {“file”: “/root/.config/caddy/autosave.json”}
2020/05/03 01:25:18.114 INFO serving initial configuration
2020/05/03 01:25:18 [INFO][cache:0xc00059f130] Started certificate maintenance routine

5. What I already tried:

Consulted documentation. Looked on GitHub. Chatted to some people in the Discord Server. Still unable to get Caddy to comply.

6. Links to relevant resources:

How to proxy Server Sent Events? · Issue #677 · caddyserver/caddy · GitHub - I can’t see the config example provided as it’s been removed :frowning:

Welcome @kittymagician -

As per the docs, try setting flush_interval -1 and see what happens.

I think you can also get rid of:

        header_up Host {http.request.host}
        header_up X-Forwarded-For {http.request.remote.host}
        header_up X-Forwarded-Proto {http.request.scheme}

You can get rid of the others too unless gunicorn actually needs them.

Posting up my config in case others get stuck.

Thank you so much for your help and speed @matt That kept me up until the early hours of the morning disabling buffering worked a charm.

demo.domain.com {
    root * /var/www/Project
    tls email@domain.com
    reverse_proxy localhost:5000 {
        flush_interval -1
    }
    header {
        X-Content-Type-Options nosnif
        X-Frame-Options DENY
        Referrer-Policy no-referrer-when-downgrade
   }
}

Thanks again!

1 Like

Great!

FYI your nosniff is missing an f.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.