title: Bokeh server_document script loads from IP (HTTP) but not from domain (HTTPS)
body: <p>I have a Bokeh application embedded in Flask and Gunicorn (see <a href="https://github.com/bokeh/bokeh/blob/master/examples/howto/server_embed/flask_gunicorn_embed.py" rel="nofollow noreferrer">flask_gunicorn_embed.py</a>). This works fine when I access everything through the IP address of the web server, but not when everything is proxied through Nginx. It will load everything from Flask, but not from Bokeh (the <code>autoload.js</code>).</p>
  
  <hr>
  
  <h3>Example:</h3>
  
  <p>I start Flask through Gunicorn with</p>
  
  <pre><code>gunicorn --workers 9 --bind 0.0.0.0:5000 --timeout 120 --log-file /some/directory/to/gunicorn/logs/gunicorn.log -m 0700 flask:app
  </code></pre>
  
  <p>I can now access everything from Flask through <em><a href="http://xxx.xxx.xxx.xxx:5000" rel="nofollow noreferrer">http://xxx.xxx.xxx.xxx:5000</a></em>. I have adapted the <em>flask_gunicorn_embed.py</em> and the most important bits are</p>
  
  <pre><code>script = server_document('http://xxx.xxx.xxx.xxx:%d/bkapp' % port, resources=None)
  sockets, port = bind_sockets("0.0.0.0", 0)
  bokeh_tornado = BokehTornado({'/bkapp': bkapp}, extra_websocket_origins=["xxx.xxx.xxx.xxx:5000"])
  </code></pre>
  
  <p>If I now access a page which uses this Bokeh server everything works fine. It loads</p>
  
  <pre><code>http://xxx.xxx.xxx.xxx:XXXXX/bkapp/autoload.js?bokeh-autoload-element=1001&amp;bokeh-app-path=/bkapp&amp;bokeh-absolute-url=http://xxx.xxx.xxx.xxx:XXXXX/bkapp&amp;resources=none
  </code></pre>
  
  <p>and displays the graphs, and creates a web socket for callbacks.</p>
  
  <hr>
  
  <p>This is <strong>without</strong> Nginx as a reverse proxy. I don't want to use the IP address of the web server because I need to use HTTPS, which requires a domain.</p>
  
  <p>Thus I have the following Nginx configuration:</p>
  
  <pre><code>server {
      listen 80;
      server_name example.com;
  
      return 301 https://$server_name/;
  }
  
  server {
      listen 443;
      server_name example.com;
  
      ssl on;
      ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  
      client_max_body_size 50M;
  
      location / {
          include proxy_params;
          proxy_pass http://127.0.0.1:5000;
          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;
          proxy_buffering off;
      }
  
      location /static {
          alias /some/directory/to/flask/static;
      }
  }
  </code></pre>
  
  <p>And I start Flask through Gunicorn with</p>
  
  <pre><code>gunicorn --workers 9 --bind 127.0.0.1:5000 --timeout 120 --log-file /some/directory/to/gunicorn/logs/gunicorn.log -m 0700 flask:app
  </code></pre>
  
  <p>I can now access everything from Flask through <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a>. The <em>flask_gunicorn_embed.py</em> now looks like</p>
  
  <pre><code>script = server_document('https://example.com:%d/bkapp' % port, resources=None)
  sockets, port = bind_sockets("0.0.0.0", 0)
  bokeh_tornado = BokehTornado({'/bkapp': bkapp}, extra_websocket_origins=["example.com"])
  </code></pre>
  
  <p>However, every request that is generated through Bokeh's <code>server_document</code></p>
  
  <pre><code>https://example.com:XXXXX/bkapp/autoload.js?bokeh-autoload-element=1001&amp;bokeh-app-path=/bkapp&amp;bokeh-absolute-url=https://example.com:XXXXX/bkapp&amp;resources=none
  </code></pre>
  
  <p>results in either a <em>connection timed out</em> or a <em>connection refused</em> and thus not loading the scripts.</p>
  
  <hr>
  
  <p>How can I make it such that it will connect and load the script?</p>
  
  <p>It must have something to do with Nginx because if I request the file through the IP address it still works (due to the <code>bind_sockets("0.0.0.0", 0)</code>). But I cannot figure out what is causing this issue.</p>
  
  <hr>
  
  <h3>Edit:</h3>
  
  <p>It appears to be an issue with the fact that it uses HTTPS. My Nginx configuration is the same as the one given in the <a href="https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#reverse-proxying-with-nginx-and-ssl" rel="nofollow noreferrer">Bokeh documentation</a>. The documentation says to use <code>--use_xheaders</code>, which is not possible because I am not using <code>bokeh serve</code>.</p>
  
  <p>I do have</p>
  
  <pre><code>conf = {'use_xheaders': True}
  bokeh_tornado = BokehTornado({'/bkapp': bkapp}, extra_websocket_origins=["example.com"], **conf)
  bokeh_http = HTTPServer(bokeh_tornado, xheaders=True)
  </code></pre>
  
  <p>but it still won't load the scripts for HTTPS pages.</p>
  
  <p><a href="http://example_no_https.com" rel="nofollow noreferrer">http://example_no_https.com</a> will load the pages through the ports and <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a> won't.</p>