-
-
Notifications
You must be signed in to change notification settings - Fork 895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working Behind an (NginX) Proxy? #22
Comments
Did you resolve this issue? I am working on a web application and it will be behind a nginx proxy as well. What are the errors you are getting? Can you please elaborate more |
The issue may be that gunicorn can't run with more than 1 worker using gevent-socketio. They are discussing it here: abourget/gevent-socketio#132 |
In all cases when you have a socket based server you use a single worker. Multiple clients are handled by gevent's greenlets. I will look into making the example work under nginx and add the config to the documentation. |
I'm not sure it is helpful. But it won't hurt anyone: This works for me without SSL (did not test it for SSL):
I got a javascript app for communicating with flask. I had to explicitly define the resource location like so to get it to work: io.connect('myurl', {resource : flask/socket.io}); Note that I got everything here setup for the url /flask/. Should be trivial to adjust though. |
I've also troubles to have Flask-SocketIO work with Nginx and gunicorn. I keep getting the following Runtime error : "You need to use a gevent-socketio server". Here is the full traceback :
Here are some details about my config :
js :
gunicorn start command :
|
Do things work when you remove nginx from the equation? |
Yes, if I open directly http://127.0.0.1:8000, everything works smoothly |
However, if I don't use Gunicorn, and run directly the application along with nginx, it doesn't work either. if __name__ == '__main__':
socketio.run(app,"127.0.0.1",8000) and then I run the application directly with python2, and I get the same runtime error |
OK. I haven't really tried to set up nginx to work with socketio, I believe that is where the root of the problem is. As soon as I figure this out I will update the documentation. |
I have tried alcinos's nginx conf. the socketio can not work on port 80. but it works on port 5000. my run command: can i add worker args to it, like "-w 4"? I have bought Miguel's book. But it does not say much about deployment on trandional hosting. I am trying to build a website on my cloud server, not heroku, but one initiated with just ubuntu server and ssh access. My website would run with flask, flask-socketio, flask-restful, gunicorn, nginx and mongodb. It is about collecting data from arduino with internet chip. |
Maybe this is a misunderstanding. The normal setup is to put nginx to listen on port 80 to external connections. Nginx then proxies the socket connections to the internal server, which runs on a high port number such as 5000. If this is what you have, then you are doing it right. The gevent based servers create and destroy workers automatically using greenlets. At the gunicorn level you need to use one server. Due to the lightweight nature of greenlets a server should be able to spawn tens of thousands of jobs concurrently, which is way more than most applications need. |
ok, so there would be no need for "-w 4" But socket.io / flask-socketio does not work directly by proxy on nginx (port 80). Look forwards to your solution / blog on this, which would complete your book also. |
Some news here, I finally got it to work as expected. Not sure what was the problem though, some possible hints would be the version of Nginx (I use 1.6.0), and a line I changed inside the conf file : I added explicit listening to ipv4 addresses. The relevant lines are listed above :
Hope that helps |
@alcinos So, should we create a new wiki page with all this? I know for sure I'll need it in the future. |
@miguelgrinberg Here is my demo: a raspberry pi in my house post my room temperature every 10 sec if it diffs from last commits. the curve would append new node if new data uploaded by flask-socketio. it works on port 5000 http://115.29.223.207:5000/sensor/538147497943f70bcf756146 but socketio does not work on port 80 (forwards by nginx): http://115.29.223.207/sensor/538147497943f70bcf756146 You may check the chrome console, and you would see socket.io runs time out all the time on port 80. |
@aguegu Maybe you could post your nginx conf so that we try to see what's wrong. |
I appreciate all the effort you guys are putting in figuring this thing out. I hope I will have some time this weekend to look into this. If I manage to make this work I will update the documentation with the recipe. |
my sites-enable/busykoala: server {
} same as @alcinos 's |
I'm happy to report success on this. I have been able to run the example app on gunicorn and/or werkzeug on localhost, while nginx proxying it to the outside world. First it is important to use a recent nginx release. I built latest version 1.7.1 from source, In theory any release 1.4 and up will work, but releases before 1.4 do not have proxy support for WebSocket. My configuration very similar to the one shown above:
If you can't still make it work then please try the latest nginx release. If that still doesn't do it, then check the javascript console and see what error(s) you get. |
@miguelgrinberg it works. It turns out that my nginx is too old on my hosting of ubuntu 12.04. It was 1.1.x. As I added ppa: nginx/stable and upgrade nginx to 1.6.0, the problem is solved and socketio works. |
can someone elaborate on these lines: location /socket.io { |
@nikitph the |
@miguelgrinberg thanks a lot miguel. big fan of your work. interesting. so do i need to supply this on the client? locally everything works very smoothly. i am above version 1.4 for nginx too. my client looks like this var socket = io.connect('http://' + document.domain + ':' + location.port + namespace); this is my local server setting manage.py --> now this is my deploy script on digital ocean exec gunicorn --timeout=300 -b 127.0.0.1:{{port}} manage:app -w $NUM_WORKERS and this is the nginx config --> server { server {
} but i get a bunch of JS errors on the client and this on the backend 2016-04-30 17:48:30 [20877] [DEBUG] GET /socket.io/ appreciate the help. |
i just saw i m loading on a closed bug. LMK if you want a new one opened. |
First, this has been a great library to use: very convenient and helpful. However, I've had a ton of trouble setting up an NginX (v1.4) reverse proxy, which is needed to handle SSL and other useful functions. Are there any examples of NginX config files that work with this library? I feel like I've tried and modified every socketio .conf variant on the internet at this point, but still can't get the handshake to work. For production use, being able to get this properly proxied is very important.
The text was updated successfully, but these errors were encountered: