uwsgi protocol magic variables

You can dynamically tune or configure various aspects of the uWSGI server using special variables passed by the web server (or in general by a uwsgi compliant client).

  • For Nginx, the uwsgi_param <name> <value>; directive is used.
  • For Apache, the SetEnv <name> <value> directive is used.

UWSGI_SCHEME

Set the URL scheme when it cannot be reliably determined. This may be used to force HTTPS (with the value https), for instance.

UWSGI_SCRIPT

Load the specified script as a new application mapped to SCRIPT_NAME. The app will obviously only be loaded once, not on each request.

  1. uwsgi_param UWSGI_SCRIPT werkzeug.testapp:test_app;
  2. uwsgi_param SCRIPT_NAME /testapp;

UWSGI_MODULE and UWSGI_CALLABLE

Load a new app (defined as module:callable) mapped into SCRIPT_NAME.

  1. uwsgi_param UWSGI_MODULE werkzeug.testapp;
  2. uwsgi_param UWSGI_CALLABLE test_app;
  3. uwsgi_param SCRIPT_NAME /testapp;

UWSGI_PYHOME

Dynamically set the Python Virtualenv support for a dynamic application.

See also

DynamicVirtualenv

UWSGI_CHDIR

chdir() to the specified directory before managing the request.

UWSGI_FILE

Load the specified file as a new dynamic app.

UWSGI_TOUCH_RELOAD

Reload the uWSGI stack when the specified file’s modification time has changed since the last request.

  1. location / {
  2. include uwsgi_params;
  3. uwsgi_param UWSGI_TOUCH_RELOAD /tmp/touchme.foo;
  4. uwsgi_pass /tmp/uwsgi.sock;
  5. }

UWSGI_CACHE_GET

See also

The uWSGI caching framework

Check the uWSGI cache for a specified key. If the value is found, it will be returned as raw HTTP output instead of the usual processing of the request.

  1. location / {
  2. include uwsgi_params;
  3. uwsgi_param UWSGI_CACHE_GET $request_uri;
  4. uwsgi_pass 127.0.0.1:3031;
  5. }

UWSGI_SETENV

Set the specified environment variable for a new dynamic app.

Note

To allow this in Python applications you need to enable the reload-os-env uWSGI option.

Dynamically load a Django app without using a WSGI file/module:

  1. location / {
  2. include uwsgi_params;
  3. uwsgi_param UWSGI_SCRIPT django.core.handlers.wsgi:WSGIHandler();
  4. uwsgi_param UWSGI_CHDIR /mydjangoapp_path;
  5. uwsgi_param UWSGI_SETENV DJANGO_SETTINGS_MODULE=myapp.settings;
  6. }

UWSGI_APPID

Note

Available since 0.9.9.

Bypass SCRIPT_NAME and VirtualHosting to let the user choose the mountpoint without limitations (or headaches).

The concept is very generic: UWSGI_APPID is the identifier of an application. If it is not found in the internal list of apps, it will be loaded.

  1. server {
  2. server_name server001;
  3. location / {
  4. include uwsgi_params;
  5. uwsgi_param UWSGI_APPID myfunnyapp;
  6. uwsgi_param UWSGI_FILE /var/www/app1.py
  7. }
  8. }
  9.  
  10. server {
  11. server_name server002;
  12. location / {
  13. include uwsgi_params;
  14. uwsgi_param UWSGI_APPID myamazingapp;
  15. uwsgi_param UWSGI_FILE /var/www/app2.py
  16. }
  17. }