Help needed with SSL for a Nginx, Gunicorn, Flask, Bokeh setup

I am not able to show the Bokeh server app when I add SSL to my Nginx proxy server; the Index page shows up with SSL, but the page with embed Bokeh server document does not show the plot. Without SSL everything works fine. I am running the webserver on an internal private network, hence the certificate created is just self signed.

I am using the --use-xheaders option when running the Bokeh server. I use server_document to embed Bokeh doc. If I in the app route for the bokeh url specify regular http:// then I get an browser error related to mixed content.

Blocked loading mixed active content “…ers&bokeh-absolute-url=”

If I instead use https:// then nothing happens (seems to be time out on the bokeh script). Browser reports in the XHR among others “No headers for this request”; (since the Bokeh server is running on http:// then this is expected I guess).

Starting Bokeh server:

bokeh serve sliders/ --prefix /bokeh --allow-websocket --address --port 5100 --use-xheaders


gunicorn --bind wsgi:app

Using Bokeh version 2.0.2 (not the latest due to bug related to some transformation), CentOS 8.

So I do not understand how to get the Nginx proxy to show the Bokeh doc with setup with SSL for Nginx part. Any help appreciated.

nginx conf

server {
    listen 443 ssl;

    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
#    # enables all versions of TLS, but not SSLv2 or v3 which are deprecated.
    ssl_protocols TLSv1.1 TLSv1.2;
#    # disables all weak ciphers
    ssl_prefer_server_ciphers on;

    # bokeh apps are run with prefix option where prefix bokeh
    location /bokeh {
        proxy_set_header    Upgrade              $http_upgrade;
        proxy_set_header    Connection           "upgrade";
        proxy_http_version  1.1;
        proxy_set_header    X-Forwarded-Proto    $scheme;
        proxy_set_header    X-Forwarded-For	 $proxy_add_x_forwarded_for;
        proxy_set_header    Host                 $host:$server_port;

    location / {
        proxy_redirect     off;
        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For	$proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;


from flask import render_template, request
from flask import current_app as app
from bokeh.embed import server_document, server_session
from bokeh.client import pull_session

# setup all the routes
def home():
    """Landing page."""
    return render_template(
        title = 'Index',

BOKEH_PREFIX = 'bokeh/'
BOKEH_PORT = '5100'

def sliders():
    title = 'Sliders'
    app_name = 'sliders'
    bk_app = '{}{}'.format(BOKEH_PREFIX, app_name)

    app_url = 'https://{}:{}/{}'.format(
    bokeh_script = server_document(url = app_url, relative_urls = False)

    return render_template("bk_app.html", bokeh_script = bokeh_script, title = title, url = app_url)


<!-- bk_app.html -->
<!doctype html>
           <a href="{{ url_for('home') }}">Index</a>
   {{ bokeh_script|safe }}
<script src=""
<script src=""
<script src=""
<script src=""


<!-- index.html -->
<!doctype html>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="{{ url_for('static', filename='css/template.css') }}">
    {% block head %}{% endblock %}
           <a href="{{ url_for('sliders') }}">Sliders</a>


from flask import Flask

def create_app():
    """Initialize the core application."""
    app = Flask(
        instance_relative_config = False,
        template_folder = 'templates',
        static_folder = 'static'

    with app.app_context():
        # Include our Routes
        from . import routes

        return app

from application import create_app

app = create_app()

if __name__ == "__main__":'', port = 8000, debug = True)

Unfortunately my nginx expertise is extremely minimal. Is this just in order to be able to have SSL connections to the Bokeh server? If so, it can be configured to directly terminate SSL without needing a proxy:

I ran into similar problems a few weeks back and put the issue aside without resolution. This topic has made me want to revisit how to configure things to get my embedded bokeh apps working securely.

My system is a subset of that identified by @Jonas_Grave_Kristens; Gunicorn+Flask+bokeh but not currently using the nginx layer.

Like @Jonas_Grave_Kristens, my routes that are not related to bokeh are accessible via HTTPS, but the bokeh document doesn’t embed properly. Depending what options I give when configuring the servers I end up with errors related to mixed-content (HTTPS with bokeh via HTTP) or problems with SSL error codes when trying to configure HTTPServer with protocol, ssl_options, etc.

Well I would like the web server itself to have SSL so yes I need Nginx to have SSL. I tried setting up bokeh using cert and key I created but I got permission denied, Bokeh server does not seem to be able to read the created certificate…


I am running bokeh 2.0.2 and generated certificates using openssl, and can get bokeh to have SSL connections when it is the only server.

And the client browser confirms that the traffic is encrypted (tested with Firefox and Safari, b/c these are easier to make security exceptions for self-signed certificates compared to Chrome).

Apologies if I misunderstand the reported behavior in the quoted text, but I think bokeh can deal with the certificates properly at least in certain system setups. I think the problem is how to make the leap from that bokeh-only system to one that embeds it.




Desktop % bokeh serve --ssl-certfile cert.pem --ssl-keyfile key.pem

2020-07-30 05:44:52,318 Starting Bokeh server version 2.0.2 (running on Tornado 6.0.4)

2020-07-30 05:44:52,319 Configuring for SSL termination

2020-07-30 05:44:52,320 User authentication hooks NOT provided (default user enabled)

2020-07-30 05:44:52,324 Bokeh app running at: http://localhost:5006/sliders

2020-07-30 05:44:52,324 Starting Bokeh server with process id: 97617

2020-07-30 05:45:33,695 WebSocket connection opened

2020-07-30 05:45:33,695 ServerConnection created

In my setup, sans the nginx part, I can run the web server securely by passing the credentials to gunicorn, which gives me secure connections to all of the other pages on the site. I just have not figured out how to configure the bokeh server part of things in my Flask app. (I am running the bokeh server programmatically in a worker thread and have tried passing things through during the server instantiation related to SSL options, etc.)

I made a specific certificate for the bokeh server and running that as you do (no Nginx) works, however due to self signed certificate I get error messages but the bokeh document is displayed (Firefox).

SSL Error on 8 ('', 45674): [SSL: SSLV3_ALERT_BAD_CERTIFICATE] sslv3 alert bad certificate (_ssl.c:897)

And now I suddenly got it working with Nginx… not sure what happend… clearing of browser cache? However Chromium/Chrome do not accept the self signed certificate.

I need to use some more time on the setup testing endpoints/routes. Also need to figure out if it is possible to create self signed certificate that works in Chrome. So far I have used openssl

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/bokeh-selfsigned.key -out /etc/ssl/certs/bokeh-selfsigned.crt


I was able to get Chrome (Version 84) to work with self-signed certificates. The important step seems to be generating a self-signed certificate with SAN (Subject Alternative Names). If you do that, the client’s browser will still generate a warning, but there will be a link that allows you to proceed. See screenshot Proceed to (unsafe) link at bottom.

I used the Bash script with config baked in procedure on this serverfault URL to generate the self-signed SAN statistic, b/c I preferred it to modifying existing openssl configuration files.

Thanks, I’ll look into this.