Enabling websockets on Nginx && some random learnings
When you use Nginx as a reverse proxy server behing a Gunicorn server, if you want to forward websocket requests — make sure to enable it on Nginx by adding the last 3 lines to your nginx.conf file in /etc/nginx/sites-available/
location / {
...
...
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;}
pkill gunicorn
Testing nginx
sudo nginx -t
If you get the error open() “/run/nginx.pid” failed (13: Permission denied)
$ ps axu | grep `cat /var/run/nginx.pid`
Those are backticks (`) and not apostrophes ('). If it isn't running, remove the pid file:
$ sudo rm /var/run/nginx.pid
and restart nginx. On many systems, that's:
$ sudo /etc/init.d/nginx restart
Make sure that in /etc/nginx/conf/nginx.conf, under the http directive, the custom conf gets picked up
include /etc/nginx/sites-enabled/*
In the nginx.conf, under the server directive,
listen proxy_protocol
breaks the header and throws a broken header: “GET / HTTP/1.1 in /var/log/nginx/error.log
So replaced it with listen 80 to get the 101 switching protocol to work and upgrade the connection to a websocket.
While using an ELB loadbalancer, listen proxy_protocol seemed to work without any issues
Clear Memory:
>> sudo sh -c “sync; echo 1 > /proc/sys/vm/drop_caches”
>> sudo sh -c “sync; echo 2 > /proc/sys/vm/drop_caches”
>> sudo sh -c “sync; echo 3 > /proc/sys/vm/drop_caches”