Flask Socket IO: Deployment Part 2














































Flask Socket IO: Deployment Part 2



It is possible to use nginx as a front-end reverse  proxy  that  passes  requests  to  the
       application.  However,  only  releases  of  nginx  1.4  and  newer support proxying of the
       WebSocket protocol. Below is a basic nginx configuration that proxies HTTP  and  WebSocket
       requests:

          server {
              listen 80;
              server_name _;

              location / {
                  include proxy_params;
                  proxy_pass http://127.0.0.1:5000;
              }

              location /static {
                  alias <path-to-your-application>/static;
                  expires 30d;
              }

              location /socket.io {
                  include proxy_params;
                  proxy_http_version 1.1;
                  proxy_buffering off;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection "Upgrade";
                  proxy_pass http://127.0.0.1:5000/socket.io;
              }
          }

       The next example adds the support for load balancing multiple Socket.IO servers:

          upstream socketio_nodes {
              ip_hash;

              server 127.0.0.1:5000;
              server 127.0.0.1:5001;
              server 127.0.0.1:5002;
              # to scale the app, just add more nodes here!
          }

          server {
              listen 80;
              server_name _;

              location / {
                  include proxy_params;
                  proxy_pass http://127.0.0.1:5000;
              }

              locaton /static {
                  alias <path-to-your-application>/static;
                  expires 30d;
              }

              location /socket.io {
                  include proxy_params;
                  proxy_http_version 1.1;
                  proxy_buffering off;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection "Upgrade";
                  proxy_pass http://socketio_nodes/socket.io;
              }
          }


Comments