Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Can't get SSE to work behind Nginx #2

Closed
malteo opened this issue Jan 23, 2014 · 6 comments
Closed

Can't get SSE to work behind Nginx #2

malteo opened this issue Jan 23, 2014 · 6 comments
Labels

Comments

@malteo
Copy link
Contributor

malteo commented Jan 23, 2014

I'm trying to serve the web interface through a Nginx reverse proxy, but the EventSource request is failing after not receiving anything for a couple of minutes with a 504 timeout.

Everything works in static interfaces (like the admin one) or when directly accessing the Django server.

These are the relevant Nginx config parts:

location ^/eventsource {
    proxy_pass http://localhost:8000;
    proxy_buffering off;
    proxy_cache off;
    proxy_set_header Host $host;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    chunked_transfer_encoding off;
}
location / {
    proxy_pass http://localhost:8000;
}

Have you managed to get it working? How's your deployment setup?

@ipmb
Copy link
Member

ipmb commented Jan 23, 2014

Hi Mateo, it seems to work well on https://botbot.me. Here's the setup we have (using uWSGI behind Nginx):

upstream eventsource-botbot-backend {
  server unix:/var/run/uwsgi/eventsource_botbot.sock fail_timeout=30s;
}
...
location /eventsource {
    include uwsgi_params;
    uwsgi_pass eventsource-botbot-backend;
    uwsgi_buffering off;
    chunked_transfer_encoding off;
    proxy_cache off;
  }

We actually run a separate uwsgi worker under gevent to serve these requests. I'm not 100% sure that is necessary, but here's the relevant settings we're using there:

gevent = 1000
async = 100
gevent-monkey-patch

Keep us posted on what you find out. We should document some recommendations here.

@malteo
Copy link
Contributor Author

malteo commented Jan 24, 2014

I'm trying a similar setup, using a single uwsgi worker

uwsgi --gevent 1000 --async 100 --gevent-monkey-patch --socket botbot.sock --wsgi-file botbot/wsgi.py

and a single location in Nginx

upstream botbot {
    server unix:///path/to/botbot.sock fail_timeout=30s;
}

location / {
    include uwsgi_params;
    uwsgi_pass botbot;
    uwsgi_buffering off;
    chunked_transfer_encoding off;
    proxy_cache off;
}

still getting the timeouts though :(

I guess I'll wait for the deployment documentation...

@ipmb
Copy link
Member

ipmb commented Jan 24, 2014

@malteo do you see the same issue on the channels on https://botbot.me ? I'd love to help you get this resolved, but want to make sure we have it right first :)

@malteo
Copy link
Contributor Author

malteo commented Jan 24, 2014

@ipmb you're right, it happens on botbot.me too! Just go on a channel without much traffic and wait ~60 seconds with (I'm using Firefox) the web console open and you'll see a 504 from Nginx and the page stops live-updating.

I guess the EventSource needs a sort of keep-alive ping or something...

@ipmb
Copy link
Member

ipmb commented Jan 24, 2014

Give this a shot...

  location /eventsource {
    include uwsgi_params;
    uwsgi_pass eventsource-botbot-backend;
    uwsgi_buffering off;
    chunked_transfer_encoding off;
    proxy_cache off;
    access_log  /var/log/nginx/eventsource_botbot.access.log;
    error_page 504 =200 @eventsource-close-graceful;
  }

  location @eventsource-close-graceful {
    add_header Content-Type text/event-stream;
    return 200;
  }

It catches the timeout and returns a response that SSE is happy with.

@ipmb ipmb closed this as completed Jan 24, 2014
@malteo
Copy link
Contributor Author

malteo commented Jan 24, 2014

Yes! Thank you

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants