Skip to main content

sticky nginx sessions and shiny

If you want to self-host your py-shiny app and your frontend is nginx this repo shows how to configure multiple shiny instances for the backend (to handle e.g. load).

This package hooks the import of the shiny app to augment it with a cookie that nginx can use to target a unique backend.

Shiny uses websockets so of course if you are behind - say - a CloudFlare firewall then this might not work at all unless websocket traffic has been enabled.

An example

The shiny testapp is based on the load balancer example from the py-shiny github and can test the correctness of the setup.

Run a foreground nginx process on port 8080 with

nginx -c $(realpath .)/templates/sticky.conf

It assumes there are 4 unix domain socket endpoints app{n}.sock in this directory.

Now run multiple background shiny instances:

# start 4 uvicorn processes holding one testapp shiny instance each
python -m shinynx.run --workers=4 testapp.core
# OR try the express version
python -m shinynx.run --workers=4 --express testapp.express

This serves as a substitute for shiny run testapp.core

You will now see app{n}.sock files appear in this directory. These are the endpoints for each of 4 shiny instances. (See the --uds option for uvicorn)

You can fire up multiple browser tabs to hit this website concurrently with:

# fire up 10 browser tabs looking at http://127.0.0.1:8080
python -m testapp.browser -n10

Note, we use unix domain sockets because they are safer (no direct internet access) and it is also easier to isolate the endpoints to the directory where the code resides.

Rationale

Shiny requires a "stickyness" i.e. it must always communicate with the same background shiny instance. So the cookie setup in shinynx/sticky.py is the crucial enhancment required along with the hash $cookie_sticky consistent; nginx configuration option.

The nginx load balancer should ensure that the processes are "sticky" using a random "sticky" cookie. There are possibly better solutions if you have the "plus" version of nginx. But this works with the open source version.

See the shiny docs.

Currently we set our cookie value to the uvicorn endpoint value (e.g. app1.sock or app2.sock etc.). But there is no guarantee that nginx will initially map a cookie value of app1.sock to the app1.sock process (it's a hash after all!).

Metadata

Release files for shinynx 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shinynx 0.1.1
File Size Uploaded Provenance Actions
shinynx-0.1.1.tar.gz 5.6 kB Without provenance Details for shinynx-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shinynx 0.1.1
File Interpreter ABI Platform Actions
shinynx-0.1.1-py3-none-any.whl Python 3 none any Details for shinynx-0.1.1-py3-none-any.whl

Total release size: 13.0 kB

Release files / shinynx-0.1.1.tar.gz

Download URL shinynx-0.1.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d27f16a73bd4c988ccbb5b4481bdfb92285ead9277967dcb6de9d4624944fa67
BLAKE2b-256 checksum
How to use checksums
a2abe48259a07246aa596448cb93041771d65385282665e6a7020c2e8f67815b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-24-generic

Release files / shinynx-0.1.1-py3-none-any.whl

Download URL shinynx-0.1.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b66c7572b99f10c51bf51de4142e9c91838344b2acd7021f2aa59bef48c4884
BLAKE2b-256 checksum
How to use checksums
55336935e29a29bbe37f6419abc023d8d41f47b5ac8c7a1e8d28fb59ddd60908
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.12.3 Linux/6.11.0-24-generic

Security for shinynx 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page