librelist archives

« back to archive

Static paths behind a proxy

Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-10 @ 07:19
Hi,

I am trying to deploy my Flask application (relies on Flask 0.6.1) in a 
situation where my employer requires me to make it public at a temporal 
location which takes the form of http://foo.bar.tld/blah/

Apache + mod_wsgi are within a virtual machine and the traffic is 
proxyed from another Apache instance in the same physical machine.

After setting up my virtual host all seems to work fine except the 
static paths.

I can get that working by setting

Alias /static/ "/path/to/my_app/static/"

in the virtual host and then changing my application initialization 
function to:

app = Flask(__name__, static_path='/blah/static')

But that's suboptimal, as I have to change the source code in order to 
change a setting that's tied to the production environment.

Ideally I would have a setting in my configuration file for this 
situation, but the application configuration comes right after the app 
object has been created, so I see no way to achieve this.

Any thoughts?

Thanks in advance,
Julen.

Re: [flask] Static paths behind a proxy

From:
Alex
Date:
2011-08-10 @ 21:10
On Wed, Aug 10, 2011 at 9:19 AM, Julen Ruiz Aizpuru <julenx@gmail.com> wrote:
> Hi,
>
> I am trying to deploy my Flask application (relies on Flask 0.6.1) in a
> situation where my employer requires me to make it public at a temporal
> location which takes the form of http://foo.bar.tld/blah/
>
> Apache + mod_wsgi are within a virtual machine and the traffic is
> proxyed from another Apache instance in the same physical machine.
>
> After setting up my virtual host all seems to work fine except the
> static paths.

Do you have this problem only with static files? How did you manage to
make dynamic URLs working?

Alex

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-11 @ 13:12
az., 2011.eko aburen 10a 23:10(e)an, Alex(e)k idatzi zuen:
> On Wed, Aug 10, 2011 at 9:19 AM, Julen Ruiz Aizpuru<julenx@gmail.com>  wrote:
>>
>> After setting up my virtual host all seems to work fine except the
>> static paths.
>
> Do you have this problem only with static files? How did you manage to
> make dynamic URLs working?
>

No, as I said on my second message all the generated URLs are failing. 
Basically the thread's subject is wrong.

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-10 @ 21:26
Hi,

On 8/10/11 11:10 PM, Alex wrote:
> Do you have this problem only with static files? How did you manage to
> make dynamic URLs working?
Unlikely.  Static files are not any different from regular views from
the the viewpoint of Flask.


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Alex
Date:
2011-08-10 @ 20:04
I have the same problem on my hosting provider (Webfaction). Every
application can be "mounted" on an URL like yourdomain.com/yourapp.
The application runs on an apache2 server that is behind an nginx
proxy.
The ugly solution I came out with is adding a PREFIX configuration
value  in the app config file than using it in my templates.
I'd really like to understand how to do it the right way.

Alex

On Wed, Aug 10, 2011 at 9:19 AM, Julen Ruiz Aizpuru <julenx@gmail.com> wrote:
> Hi,
>
> I am trying to deploy my Flask application (relies on Flask 0.6.1) in a
> situation where my employer requires me to make it public at a temporal
> location which takes the form of http://foo.bar.tld/blah/
>
> Apache + mod_wsgi are within a virtual machine and the traffic is
> proxyed from another Apache instance in the same physical machine.
>
> After setting up my virtual host all seems to work fine except the
> static paths.
>
> I can get that working by setting
>
> Alias /static/ "/path/to/my_app/static/"
>
> in the virtual host and then changing my application initialization
> function to:
>
> app = Flask(__name__, static_path='/blah/static')
>
> But that's suboptimal, as I have to change the source code in order to
> change a setting that's tied to the production environment.
>
> Ideally I would have a setting in my configuration file for this
> situation, but the application configuration comes right after the app
> object has been created, so I see no way to achieve this.
>
> Any thoughts?
>
> Thanks in advance,
> Julen.
>

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-10 @ 21:04
Hi,

On 2011-08-10 10:04 PM, Alex wrote:
> I have the same problem on my hosting provider (Webfaction). Every
> application can be "mounted" on an URL like yourdomain.com/yourapp.
> The application runs on an apache2 server that is behind an nginx
> proxy.
> The ugly solution I came out with is adding a PREFIX configuration
> value  in the app config file than using it in my templates.
> I'd really like to understand how to do it the right way.
Write a WSGI middleware that prefixes SCRIPT_NAME with that prefix


class WebFactionMiddleware(object):
     def __init__(self, app):
         self.app = app
     def __call__(self, environ, start_response):
         environ['SCRIPT_NAME'] = '/yourapp'
         return self.app(environ, start_response)

app.wsgi_app = WebFactionMiddleware(app.wsgi_app)


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-25 @ 08:19
Hi,

az., 2011.eko aburen 10a 23:04(e)an, Armin Ronacher(e)k idatzi zuen:
> Hi,
>
> On 2011-08-10 10:04 PM, Alex wrote:
>> I have the same problem on my hosting provider (Webfaction). Every
>> application can be "mounted" on an URL like yourdomain.com/yourapp.
>> The application runs on an apache2 server that is behind an nginx
>> proxy.
>> The ugly solution I came out with is adding a PREFIX configuration
>> value  in the app config file than using it in my templates.
>> I'd really like to understand how to do it the right way.
> Write a WSGI middleware that prefixes SCRIPT_NAME with that prefix
>
>
> class WebFactionMiddleware(object):
>       def __init__(self, app):
>           self.app = app
>       def __call__(self, environ, start_response):
>           environ['SCRIPT_NAME'] = '/yourapp'
>           return self.app(environ, start_response)
>
> app.wsgi_app = WebFactionMiddleware(app.wsgi_app)


Just more information for the record:

Now that I thought I had all URLs working properly with this setup 
(Flask-Assets has already been fixed), external URLs showed the proxy's 
internal hostname, so I had to use Werkzeug's ProxyFix fixer as 
described in the docs:
http://flask.pocoo.org/docs/deploying/others/#proxy-setups

Re: [flask] Static paths behind a proxy

From:
Alex
Date:
2011-08-10 @ 22:03
On Wed, Aug 10, 2011 at 11:04 PM, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 2011-08-10 10:04 PM, Alex wrote:
>> I have the same problem on my hosting provider (Webfaction). Every
>> application can be "mounted" on an URL like yourdomain.com/yourapp.
>> The application runs on an apache2 server that is behind an nginx
>> proxy.
>> The ugly solution I came out with is adding a PREFIX configuration
>> value  in the app config file than using it in my templates.
>> I'd really like to understand how to do it the right way.
> Write a WSGI middleware that prefixes SCRIPT_NAME with that prefix
>
>
> class WebFactionMiddleware(object):
>     def __init__(self, app):
>         self.app = app
>     def __call__(self, environ, start_response):
>         environ['SCRIPT_NAME'] = '/yourapp'
>         return self.app(environ, start_response)
>
> app.wsgi_app = WebFactionMiddleware(app.wsgi_app)
>

Thanks a lot Armin, it works!
Can I create a snippet from this code? I think many others less
experienced Webfaction users (like me) could benefit from it.

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-10 @ 22:37
Hi,

On 8/11/11 12:03 AM, Alex wrote:
> Thanks a lot Armin, it works!
> Can I create a snippet from this code? I think many others less
> experienced Webfaction users (like me) could benefit from it.
Sure.  There is a deployment snippet category for this kind of stuff:
http://flask.pocoo.org/snippets/category/deployment/


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Ron DuPlain
Date:
2011-08-10 @ 14:51
Hi,

On Wed, Aug 10, 2011 at 3:19 AM, Julen Ruiz Aizpuru <julenx@gmail.com> wrote:
> Alias /static/ "/path/to/my_app/static/"
>
> in the virtual host and then changing my application initialization
> function to:
>
> app = Flask(__name__, static_path='/blah/static')

In this situation, I serve the static paths directly from httpd,
instead of proxying the static file handler.  If the application
static path gets messy (e.g. with a versioned egg path), I sync to a
simpler static path in my deploy script.

-Ron

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-10 @ 18:49
az., 2011.eko aburen 10a 16:51(e)an, Ron DuPlain(e)k idatzi zuen:
> Hi,
>
> On Wed, Aug 10, 2011 at 3:19 AM, Julen Ruiz Aizpuru<julenx@gmail.com>  wrote:
>> Alias /static/ "/path/to/my_app/static/"
>>
>> in the virtual host and then changing my application initialization
>> function to:
>>
>> app = Flask(__name__, static_path='/blah/static')
>
> In this situation, I serve the static paths directly from httpd,
> instead of proxying the static file handler.  If the application
> static path gets messy (e.g. with a versioned egg path), I sync to a
> simpler static path in my deploy script.
>

Hm not sure what all this means, but I have to add that URL generation 
via url_for() doesn't work either, so all the links in the app are 
broken. Essentially I'm missing the '/blah' prefix in all my generated URLs.

Maybe I have to add the prefix by editing PATH_INFO via some kind of 
middleware? I'm lost.

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-10 @ 21:09
Hi,

On 2011-08-10 8:49 PM, Julen Ruiz Aizpuru wrote:
> Maybe I have to add the prefix by editing PATH_INFO via some kind of
> middleware? I'm lost.
Yes.  Also see the reply below to Alex.


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-11 @ 13:12
az., 2011.eko aburen 10a 23:09(e)an, Armin Ronacher(e)k idatzi zuen:
> Hi,
>
> On 2011-08-10 8:49 PM, Julen Ruiz Aizpuru wrote:
>> Maybe I have to add the prefix by editing PATH_INFO via some kind of
>> middleware? I'm lost.
> Yes.  Also see the reply below to Alex.
>

Thanks.

I have used that middleware and now the links are correct except for the 
static files — they're still broken. Is that the expected behaviour?

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-11 @ 13:16
Hi,

On 2011-08-11 3:12 PM, Julen Ruiz Aizpuru wrote:
> I have used that middleware and now the links are correct except for the
> static files — they're still broken. Is that the expected behaviour?
Can you paste the config for your server, webfaction and the relevant 
parts of the app?  This is not intended behavior and I can't see how 
static files should fail if the rest works.


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-11 @ 14:26
og., 2011.eko aburen 11a 15:16(e)an, Armin Ronacher(e)k idatzi zuen:
> Hi,
>
> On 2011-08-11 3:12 PM, Julen Ruiz Aizpuru wrote:
>> I have used that middleware and now the links are correct except for the
>> static files — they're still broken. Is that the expected behaviour?
> Can you paste the config for your server, webfaction and the relevant
> parts of the app?  This is not intended behavior and I can't see how
> static files should fail if the rest works.

It seems Flask-Assets is causing this strange behaviour.

If I use url_for directly in my templates the static URL is correctly 
processed, but when using Flask-Assets the URL is not prefixed. 
Flask-Assets uses url_for internally[1] so I guess this should also be 
prefixed... or not?


[1] 
https://github.com/miracle2k/flask-assets/blob/master/src/flaskext/assets.py#L131

Re: [flask] Static paths behind a proxy

From:
Armin Ronacher
Date:
2011-08-11 @ 15:20
Hi,

On 2011-08-11 4:26 PM, Julen Ruiz Aizpuru wrote:
> It seems Flask-Assets is causing this strange behaviour.
Indeed.  It should use the regular request context and not create its 
own if there is already one.  File a bug against it.


Regards,
Armin

Re: [flask] Static paths behind a proxy

From:
Julen Ruiz Aizpuru
Date:
2011-08-11 @ 18:05
og., 2011.eko aburen 11a 17:20(e)an, Armin Ronacher(e)k idatzi zuen:
> Hi,
>
> On 2011-08-11 4:26 PM, Julen Ruiz Aizpuru wrote:
>> It seems Flask-Assets is causing this strange behaviour.
> Indeed.  It should use the regular request context and not create its
> own if there is already one.  File a bug against it.

Will do that. Thanks for your help :)