Quantcast

Re:Reverse-proxying: Flask app with Bokeh server on Nginx

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re:Reverse-proxying: Flask app with Bokeh server on Nginx

nginx mailing list
Message: 2
Date: Fri, 12 May 2017 13:33:16 +0300
From: "Reinis Rozitis" <[hidden email]>
To: <[hidden email]>
Subject: Re: Reverse-proxying: Flask app with Bokeh server on Nginx
Message-ID: <EE53C663F45C44159B5944975E0514DF@Neiroze>
Content-Type: text/plain; format=flowed; charset="UTF-8";
        reply-type=original

> I understand that I might have to use a method called reverse proxying,
> which is described here. However, I wasn't able to get it to work.

Well you already do this "method called reverse proxying" with the Flask app
so you have to do the same with the Bokeh app as all modern/current browsers
require all resources on a HTTPS website to be also loaded through secure
channels.


> Does anybody have an idea how to solve this? A similar problem was
> described here.

You can basically copy the configuration from the SO thread you linked
(obviously you can change the location names as you wish / they just need to
match):

In nginx add:

location /bokeh/ {
    proxy_pass http://127.0.1.1:5006;

    # .. with the rest of directives
}

relaunch the Bokeh app with

--prefix=/bokeh/

and (if takes part in the url construction rather than application
background requests) change the url variable in the Flask app

url='http://###.###.###.##:5006'
to
url='https://yourserver/bokeh/'

or even just relative url='/bokeh/' .. (I'm not familiar with this software
stack so you have to test yourself)


Thanks Reinis for you quick reply.

I did the changes you suggested. 
1. in '/etc/nginx/sites-available/default' I added a new location as follow:
location /bokeh/ {
                   proxy_pass http://127.0.0.1:5006;  # you suggested 127.0.1.1, but I figured that was a typo
                   proxy_redirect off;
                   proxy_set_header Host $http_host;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
2.  in '/etc/supervisor/conf.d/bokeh_serve.conf' I added --prefix=/bokeh/:
[program:bokeh_serve]
command=/opt/envs/virtual/bin/bokeh serve company_abc.py company_xyz.py geomorphix.py --prefix=/bokeh/ --allow-websocket-origin=www.example.com --allow-websocket-origin=example.com --host=138.197.132.46:5006 --use-xheaders
directory=/opt/webapps/flask_telemetry
autostart=false
autorestart=true
startretries=3
user=nobody
 3. in the Flask app, I changed the URL to:

Now, when I open the app in the browser I get a 502 Bad Gateway, and the Flask log file says the following: 
raise IOError("Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)")
IOError: Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)
However, when I go to the page http://###.###.##.##:5006/bokeh/geomorphix, the app works fine.

Do you have an idea what's going on?

Cheers,
Julian

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re:Reverse-proxying: Flask app with Bokeh server on Nginx

Reinis Rozitis
>  3. in the Flask app, I changed the URL
> to:url='https://138.197.132.46:5006/bokeh/'
> Now, when I open the app in the browser I get a 502 Bad Gateway, and the
> Flask log file says the following:
> raise IOError("Cannot pull session document because we failed to connect
> to the server (to start the server, try the 'bokeh serve' command)")

Well seems that the Flask app uses the url also for background requests.

You can't mix 'https://' and :5006 port  in same url - this way the request
goes to port 5006 but it expects to be also encrypted but if I understand
correctly bokeh doesn't support SSL.


p.s. for best performance you could tweak that the Flask->bokeh requests go
through http but for the html template/output sent to clients there is
another variable or relative paths.


rr

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re:Reverse-proxying: Flask app with Bokeh server on Nginx

Reinis Rozitis
In reply to this post by nginx mailing list
What I forgot to add you need to change the 'url' (note the domain part) to:

url='https://yourdomain/bokeh/'

by looking at your error messages it seems that the 'url' is also directly
used for client requests (probably placed in the html templated) - which
means you can't use plain IP because then the browser most likely will just
generate a SSL certificate and domain mismatch.


rr

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reverse-proxying: Flask app with Bokeh server on Nginx

Francis Daly
In reply to this post by nginx mailing list
On Fri, May 12, 2017 at 04:28:12PM +0200, J K via nginx wrote:

Hi there,

> > location /bokeh/ {
> >     proxy_pass <a href="http://127.0.1.1:5006;">http://127.0.1.1:5006;
> >
> >     # .. with the rest of directives
> > }
> >
> > relaunch the Bokeh app with
> >
> > --prefix=/bokeh/
> >
> > and (if takes part in the url construction rather than application
> > background requests) change the url variable in the Flask app
> >
> > url='<a href="http://###.###.###.##:5006'">http://###.###.###.##:5006'
> > to
> > url='https://yourserver/bokeh/'

> 1. in '/etc/nginx/sites-available/default' I added a new location as follow:
>
> location /bokeh/ {
>
>                    proxy_pass <a href="http://127.0.0.1:5006;">http://127.0.0.1:5006;  # you suggested 127.0.
> *1*.1, but I figured that was a typo

The proxy_pass address should be wherever your "bokeh" http server is
actually listening.

Which probably means that whatever you use up there...

> command=/opt/envs/virtual/bin/bokeh serve company_abc.py company_xyz.py
> geomorphix.py --prefix=/bokeh/ --allow-websocket-origin=www.example.com
> --allow-websocket-origin=example.com --host=138.197.132.46:5006
> --use-xheaders

you should also use up there as --host.

I suspect that making them both be 127.0.0.1 will be the easiest
way of reverse-proxying things; but I also suspect that the
"--allow-websocket-origin" part suggests that you may want to configure
nginx to reverse proxy the web socket connection too. Notes are at
http://nginx.org/en/docs/http/websocket.html

It will be helpful to have a very clear picture of what talks to what,
when things are working normally; that should make it easier to be
confident that the same links are in place with nginx in the mix.

Good luck with it,

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Loading...