Build a dynamic proxy using RPC and internal routing

Work in progress (requires uWSGI 1.9.14, we use PyPy as the engine)

step 1: build your mapping function

we use the hostname as the mapping (you can use whatever you need)

  1. import uwsgi
  2.  
  3. def my_mapper(hostname):
  4. return "127.0.0.1:3031"
  5.  
  6. uwsgi.register_rpc('the_mapper', my_mapper)

save it as myfuncs.py

step 2: building a routing table

  1. [uwsgi]
  2. ; enable the pypy engine
  3. pypy-home = /opt/pypy
  4. ; execute the myfuncs.py file (the 'the_mapper' rpc function will be registered)
  5. pypy-exec = myfuncs.py
  6.  
  7. ; bind to a port
  8. http-socket = :9090
  9.  
  10. ; let's define our routing table
  11.  
  12. ; at every request (route-run execute the action without making check, use it instead of --route .*) run the_mapper passing HTTP_HOST as argument
  13. ; and place the result in the MYNODE variable
  14. route-run = rpcvar:MYNODE the_mapper ${HTTP_HOST}
  15. ; print the MYNODE variable (just for fun)
  16. route-run = log:${MYNODE}
  17. ; proxy the request to the chosen backend node
  18. route-run = http:${MYNODE}
  19.  
  20. ; enable offloading for automagic non-blocking behaviour
  21. ; a good value for offloading is the number of cpu cores
  22. offload-threads = 2