Google OAuth2 authlayer using nginx proxy in just 127 lines of code

We deployed our streamlit apps behind the oauth2-proxy that gives us a secure layer in front of streamlit and is easy to setup.

The oauth2 provider is Google Workspace in our case. But you can use anything that is compatible with oauth2-proxy azure, facebook, github etc. The docs are good enough to guide you through setup on google / azure end.
We are using certbot webroot plugin for SSL certs see the docs for the exact command, the certbot is installed via snap on the host system.

Here is our docker-compose and nginx config, search for YOUR… to find places where you need to get the config changed.


version: "3"
    image: nginx
      - ./nginx.conf:/etc/nginx/nginx.conf
      - /etc/letsencrypt:/etc/letsencrypt:ro
      - /var/www/html:/var/www/html:ro # for certbot webroot plugin
      - /tmp/empty:/docker-entrypoint.d/
      - 443:443
      - 80:80
    command:  nginx-debug -g 'daemon off;'
      - TZ=Europe/Warsaw
      - 4180
      - TZ=Europe/Warsaw
      - OAUTH2_PROXY_UPSTREAMS=http://streamlit:8501/

      # change that to your google domain
      # - OAUTH2_PROXY_AUTHENTICATED_EMAILS_FILE=/your-file-with-emails.txt
      # set this up to values you get from google auth


    image: "YOUR-streamlit-app"
    command: streamlit run /src/
      - TZ=Europe/Warsaw


user www-data;
worker_processes auto;
pid /run/;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;

http {

	# copied from default nginx setup

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	gzip on;

	server {
        # set up SSL
		listen 80 default_server;
		listen [::]:80 default_server;
		listen 443 ssl; # managed by Certbot

		server_name "";
		ssl_certificate /etc/letsencrypt/live/;
		ssl_certificate_key /etc/letsencrypt/live/;
		include /etc/letsencrypt/options-ssl-nginx.conf;
		ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

		root /var/www/html;
		index index.html index.htm;

		location /.well-known {
			try_files $uri $uri/ =404;

		location /stream {
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $host;

			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";

			proxy_pass http://backend;

		location / {
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $host;

			proxy_pass http://backend;
			proxy_http_version 1.1;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";

	upstream backend {
		server oauth2:4180;

Once you have the proxy deployed you can access a logged in user email address as follows:

def get_loggedin_user():
    """return logged in user email adress or None if no headers were found"""
    headers = get_request_headers()  # your way to access request headers
    return headers.get('X-Forwarded-Email', None) 

I’m not sure if it is the best way nowadays to access the request headers but it is what worked for us:

def get_session_id():
    ctx = st.report_thread.get_report_ctx()
    if ctx:
        return ctx.session_id

def get_session_info():
    session_id = get_session_id()
    if get_session_id():
        return st.server.server.Server.get_current()._get_session_info(session_id)
    raise RuntimeError('No streamlit session')

def get_request_headers():
        return get_session_info().ws.request.headers
    except AttributeError:
        return {}

Thank you so much for this!


Thanks for this great info! Must one use Docker though? Or can this easily be adapted to support a typical Heroku deployment straight from github? I am looking for relatively straightforward OAuth multi-provider solution to secure my Streamlit app on either Heroku or Digital Ocean. It’s really a shame that there are really any turnkey solutions available for deploying externally deployed Streamlit apps securely. I realize that Streamlit for Teams is now available but, as a startup, the per month price is fairly steep for a project that’s just getting off the ground. Thanks!

nothing depends on docker here, it is just a way to install nginx and oauth2-proxy that is somehow platform independent. But I’m not sure if you can have this kind of flexibility to install both on hieroku.

Hertzer have server auctions that starts at 28 usd / month without upfront cost and you get a okeish bare metal server that you can use for a lot of stuff. It is what we do for our internal apps.

If you want to stick with hieroku I would have a look at the cloudflare they have some confusing products that seems to be doing authentication of other apps, that seems to be replacing oauthproxy that they were building some time ago: GitHub - cloudflare/nginx-google-oauth: Lua module to add Google OAuth to nginx.

I say confusing as I’ve spend like 20 min trying to figure out which one you can use and I failed :), but it seems to have the auth you are looking for:

If you manage to make it work please share in this thread.

Hi PiotrCzapla,
Awesome solution! Thank you for sharing! I have a question: how does a user logout?

Logout seems to be implemented here is the commit:

We relay on the cookie expiration for logout so I don’t have snippet to implement this.

but it seems you simply go to /sign_out and you get logged out.

Thank you! I tried to redirect streamlit page to /sign_out, but it opens a new tab for user to login. The original streamlit tab is still there. Do you have any idea of redirect streamlit page to /sign_out in the same tab?

I think I figure it out by myself. Thank you!
I have another question about auth2-proxy: how to replace the auth2-proxy icon on the sign on page to my own icon?