librelist archives

« back to archive

[flask] URL prefix

[flask] URL prefix

From:
William Huang
Date:
2013-02-22 @ 08:12
I'm running a server right now where the flask app only gets things from a
www.host.com/prefix .

However, for portability and future proofing reasons I want all my routes
to look fairly standard (/, /users, etc). Right now I have nginx taking
care of this, but if I want to have one part of my flask app redirect to
another, I have to hardocde in the /prefix part of the url.

Is there a better way to do this? I looked in the flask config
documentation, and there is an application root, but that is only for
cookie sessions.

- Will

-- 
*Fortune favors the bold*

Re: [flask] URL prefix

From:
Trey Long
Date:
2013-02-22 @ 14:21
You need this:
http://flask.pocoo.org/snippets/35/

That will enable you to think of your Flask app as being at the root but 
still host it wherever you need from nginx.

On Feb 22, 2013, at 3:12 AM, William Huang <wwhuang@umich.edu> wrote:

> I'm running a server right now where the flask app only gets things from
a www.host.com/prefix . 
> 
> However, for portability and future proofing reasons I want all my 
routes to look fairly standard (/, /users, etc). Right now I have nginx 
taking care of this, but if I want to have one part of my flask app 
redirect to another, I have to hardocde in the /prefix part of the url. 
> 
> Is there a better way to do this? I looked in the flask config 
documentation, and there is an application root, but that is only for 
cookie sessions. 
> 
> - Will 
> 
> -- 
> Fortune favors the bold

Re: [flask] URL prefix

From:
Audrius Kažukauskas
Date:
2013-02-22 @ 16:17
On Fri, 2013-02-22 at 03:12:32 -0500, William Huang wrote:
> I'm running a server right now where the flask app only gets things from a
> www.host.com/prefix .
> 
> However, for portability and future proofing reasons I want all my routes
> to look fairly standard (/, /users, etc). Right now I have nginx taking
> care of this, but if I want to have one part of my flask app redirect to
> another, I have to hardocde in the /prefix part of the url.
> 
> Is there a better way to do this? I looked in the flask config
> documentation, and there is an application root, but that is only for
> cookie sessions.

Check <http://flask.pocoo.org/snippets/35/>, it seems to be exactly what
you're looking for.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] URL prefix

From:
Markus Unterwaditzer
Date:
2013-02-23 @ 00:20
"Audrius Kažukauskas" <audrius@neutrino.lt> wrote:

>On Fri, 2013-02-22 at 03:12:32 -0500, William Huang wrote:
>> I'm running a server right now where the flask app only gets things
>from a
>> www.host.com/prefix .
>> 
>> However, for portability and future proofing reasons I want all my
>routes
>> to look fairly standard (/, /users, etc). Right now I have nginx
>taking
>> care of this, but if I want to have one part of my flask app redirect
>to
>> another, I have to hardocde in the /prefix part of the url.
>> 
>> Is there a better way to do this? I looked in the flask config
>> documentation, and there is an application root, but that is only for
>> cookie sessions.
>
>Check <http://flask.pocoo.org/snippets/35/>, it seems to be exactly
>what
>you're looking for.
>
>-- 
>Audrius Kažukauskas
>http://neutrino.lt/

This does much more than he needs and might be a security risk, since 
clients then can override the scheme by setting custom headers.

-- Markus

Re: [flask] URL prefix

From:
Audrius Kažukauskas
Date:
2013-02-23 @ 13:42
On Sat, 2013-02-23 at 01:20:00 +0100, Markus Unterwaditzer wrote:
> This does much more than he needs and might be a security risk, since
> clients then can override the scheme by setting custom headers.

Isn't this taken care of by the line

  proxy_set_header X-Scheme $scheme;

in provided nginx config example?

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] URL prefix

From:
Markus Unterwaditzer
Date:
2013-02-23 @ 14:26
"Audrius Kažukauskas" <audrius@neutrino.lt> wrote:

>On Sat, 2013-02-23 at 01:20:00 +0100, Markus Unterwaditzer wrote:
>> This does much more than he needs and might be a security risk, since
>> clients then can override the scheme by setting custom headers.
>
>Isn't this taken care of by the line
>
>  proxy_set_header X-Scheme $scheme;
>
>in provided nginx config example?
>
>-- 
>Audrius Kažukauskas
>http://neutrino.lt/

Yes, if he uses that config. But why would he need that at all?

-- Markus

Re: [flask] URL prefix

From:
Audrius Kažukauskas
Date:
2013-02-23 @ 16:08
On Sat, 2013-02-23 at 15:26:57 +0100, Markus Unterwaditzer wrote:
> Yes, if he uses that config. But why would he need that at all?

Yeah, he probably won't need anything other than setting SCRIPT_NAME.  I
was just curious about possible security risk.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] URL prefix

From:
Markus Unterwaditzer
Date:
2013-02-22 @ 14:28
On 2013-02-22 09:12, William Huang wrote:
> I'm running a server right now where the flask app only gets things
> from a www.host.com/prefix [1] . 
> 
> However, for portability and future proofing reasons I want all my
> routes to look fairly standard (/, /users, etc). Right now I have
> nginx taking care of this, but if I want to have one part of my flask
> app redirect to another, I have to hardocde in the /prefix part of the
> url. 
> 
> Is there a better way to do this? I looked in the flask config
> documentation, and there is an application root, but that is only for
> cookie sessions. 
> 

I don't know if Flask has its own configuration option for this, but i 
think you can always use WSGI middleware to explicitly tell the app the 
URL prefix:

class PrefixFixer(object):
     def __init__(self, app):
         self.app = app

     def __call__(self, environ, start_response):
         environ['SCRIPT_NAME'] = 'prefix'
         return self.app(environ, start_response)

app = Flask(...)
app.wsgi_app = PrefixFixer(app.wsgi_app)

Related: https://github.com/mitsuhiko/flask/issues/680

Re: [flask] URL prefix

From:
cbrueggenolte
Date:
2013-02-22 @ 22:39
thanks guys,

I will try that after the weekend. Awesome :)
--
Carsten Brueggenolte
http://cbrueggenolte.de



2013/2/22 Markus Unterwaditzer <markus@unterwaditzer.net>:
> On 2013-02-22 09:12, William Huang wrote:
>> I'm running a server right now where the flask app only gets things
>> from a www.host.com/prefix [1] .
>>
>> However, for portability and future proofing reasons I want all my
>> routes to look fairly standard (/, /users, etc). Right now I have
>> nginx taking care of this, but if I want to have one part of my flask
>> app redirect to another, I have to hardocde in the /prefix part of the
>> url.
>>
>> Is there a better way to do this? I looked in the flask config
>> documentation, and there is an application root, but that is only for
>> cookie sessions.
>>
>
> I don't know if Flask has its own configuration option for this, but i
> think you can always use WSGI middleware to explicitly tell the app the
> URL prefix:
>
> class PrefixFixer(object):
>      def __init__(self, app):
>          self.app = app
>
>      def __call__(self, environ, start_response):
>          environ['SCRIPT_NAME'] = 'prefix'
>          return self.app(environ, start_response)
>
> app = Flask(...)
> app.wsgi_app = PrefixFixer(app.wsgi_app)
>
> Related: https://github.com/mitsuhiko/flask/issues/680

Re: [flask] URL prefix

From:
Steven Kryskalla
Date:
2013-02-22 @ 15:36
On Fri, Feb 22, 2013 at 12:12 AM, William Huang <wwhuang@umich.edu> wrote:
> I'm running a server right now where the flask app only gets things from a
> www.host.com/prefix .
>
> However, for portability and future proofing reasons I want all my routes to
> look fairly standard (/, /users, etc). Right now I have nginx taking care of
> this, but if I want to have one part of my flask app redirect to another, I
> have to hardocde in the /prefix part of the url.
>
> Is there a better way to do this? I looked in the flask config
> documentation, and there is an application root, but that is only for cookie
> sessions.

Try one of these:


http://flask.pocoo.org/mailinglist/archive/2010/7/21/running-behind-proxy-with-url-prefix-other-than/
http://flask.pocoo.org/snippets/35/
http://flask.pocoo.org/snippets/84/

You could also write your entire app in a blueprint, then use the
url_prefix option to mount it at /prefix.

Re: [flask] URL prefix

From:
William Huang
Date:
2013-02-23 @ 00:18
Thanks for all the replies! I think the reverse proxy snippet is exactly
what I need.


On Fri, Feb 22, 2013 at 10:36 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Fri, Feb 22, 2013 at 12:12 AM, William Huang <wwhuang@umich.edu> wrote:
> > I'm running a server right now where the flask app only gets things from
> a
> > www.host.com/prefix .
> >
> > However, for portability and future proofing reasons I want all my
> routes to
> > look fairly standard (/, /users, etc). Right now I have nginx taking
> care of
> > this, but if I want to have one part of my flask app redirect to
> another, I
> > have to hardocde in the /prefix part of the url.
> >
> > Is there a better way to do this? I looked in the flask config
> > documentation, and there is an application root, but that is only for
> cookie
> > sessions.
>
> Try one of these:
>
>
> 
http://flask.pocoo.org/mailinglist/archive/2010/7/21/running-behind-proxy-with-url-prefix-other-than/
> http://flask.pocoo.org/snippets/35/
> http://flask.pocoo.org/snippets/84/
>
> You could also write your entire app in a blueprint, then use the
> url_prefix option to mount it at /prefix.
>



-- 
*Fortune favors the bold*