librelist archives

« back to archive

Problem with Nginx + gunicorn + flask

Problem with Nginx + gunicorn + flask

From:
Stephane Wirtel
Date:
2011-03-04 @ 00:22
Hi all,

I want to use nginx with gunicorn and flask but I have a problem when I 
try to use the redirect function, I don't have the real host.

As you know, I use gunicorn for the flask application and nginx is the 
web-server for the cache, etc...

Internet <-> Nginx <-> Gunicorn <-> Flask
                   <-> Static Files

Here is my gunicorn config file

gunicorn.conf
~~~~~~~~~~~~~
workers = 2
bind='127.0.0.1:18000'
proc_name = 'www.example.com'
pidfile='/tmp/www.example.com.pid'

deploy.py
~~~~~~~~~
from main import create_app
from main import ProductionConfig

__all__ = ['application']

application = create_app(ProductionConfig())

The command line to execute the gunicorn server

$> gunicorn --log-level=debug -c gunicorn.conf deploy:application


nginx
~~~~~
The config for the domain: /etc/nginx/sites-enabled/www.example.com
  http://paste.pocoo.org/show/347762/

The configuration of nginx /etc/nginx/nginx.conf
  http://paste.pocoo.org/show/347763/

The problem is with the "redirect" function and the url_for.

In fact, in my flask app, if I use redirect(url_for('index')), the URL 
will be the "bind" address of gunicorn, in this case: 127.0.0.1:18000 and 
I get this url in my browser.
but not with _external=True argument of the url_for function.

I want to know where the problem is and if it's in the config of gunicorn 
or nginx, I have a doubt with my nginx conf, but I'm not sure.

Could you help me ?

Regards,

Stéphane



Re: [flask] Problem with Nginx + gunicorn + flask

From:
Sebastien Estienne
Date:
2011-03-04 @ 00:43
Hello

have you read this:
http://flask.pocoo.org/docs/deploying/others/#proxy-setups

But i don't think i need this in my setup of gunicorn + nginx.

Have you tried proxy_redirect off in nginx?

On a side note, your statics should be handle directly by nginx bypassing flask

Sebastien Estienne



On Fri, Mar 4, 2011 at 01:22, Stephane Wirtel <stephane@wirtel.be> wrote:
> Hi all,
>
> I want to use nginx with gunicorn and flask but I have a problem when I 
try to use the redirect function, I don't have the real host.
>
> As you know, I use gunicorn for the flask application and nginx is the 
web-server for the cache, etc...
>
> Internet <-> Nginx <-> Gunicorn <-> Flask
>                   <-> Static Files
>
> Here is my gunicorn config file
>
> gunicorn.conf
> ~~~~~~~~~~~~~
> workers = 2
> bind='127.0.0.1:18000'
> proc_name = 'www.example.com'
> pidfile='/tmp/www.example.com.pid'
>
> deploy.py
> ~~~~~~~~~
> from main import create_app
> from main import ProductionConfig
>
> __all__ = ['application']
>
> application = create_app(ProductionConfig())
>
> The command line to execute the gunicorn server
>
> $> gunicorn --log-level=debug -c gunicorn.conf deploy:application
>
>
> nginx
> ~~~~~
> The config for the domain: /etc/nginx/sites-enabled/www.example.com
>  http://paste.pocoo.org/show/347762/
>
> The configuration of nginx /etc/nginx/nginx.conf
>  http://paste.pocoo.org/show/347763/
>
> The problem is with the "redirect" function and the url_for.
>
> In fact, in my flask app, if I use redirect(url_for('index')), the URL 
will be the "bind" address of gunicorn, in this case: 127.0.0.1:18000 and 
I get this url in my browser.
> but not with _external=True argument of the url_for function.
>
> I want to know where the problem is and if it's in the config of 
gunicorn or nginx, I have a doubt with my nginx conf, but I'm not sure.
>
> Could you help me ?
>
> Regards,
>
> Stéphane
>
>
>
>
>

Re: [flask] Problem with Nginx + gunicorn + flask

From:
Stephane Wirtel
Date:
2011-03-04 @ 12:28
Hi Sebastien,

Thank you so much, it works fine now.

Stéphane
On 03/04/2011 01:43 AM, Sebastien Estienne wrote:
> Hello
> 
> have you read this:
> http://flask.pocoo.org/docs/deploying/others/#proxy-setups
> 
> But i don't think i need this in my setup of gunicorn + nginx.
> 
> Have you tried proxy_redirect off in nginx?
> 
> On a side note, your statics should be handle directly by nginx bypassing flask
> 
> Sebastien Estienne
> 
> 
> 
> On Fri, Mar 4, 2011 at 01:22, Stephane Wirtel <stephane@wirtel.be> wrote:
>> Hi all,
>>
>> I want to use nginx with gunicorn and flask but I have a problem when I
try to use the redirect function, I don't have the real host.
>>
>> As you know, I use gunicorn for the flask application and nginx is the 
web-server for the cache, etc...
>>
>> Internet <-> Nginx <-> Gunicorn <-> Flask
>>                   <-> Static Files
>>
>> Here is my gunicorn config file
>>
>> gunicorn.conf
>> ~~~~~~~~~~~~~
>> workers = 2
>> bind='127.0.0.1:18000'
>> proc_name = 'www.example.com'
>> pidfile='/tmp/www.example.com.pid'
>>
>> deploy.py
>> ~~~~~~~~~
>> from main import create_app
>> from main import ProductionConfig
>>
>> __all__ = ['application']
>>
>> application = create_app(ProductionConfig())
>>
>> The command line to execute the gunicorn server
>>
>> $> gunicorn --log-level=debug -c gunicorn.conf deploy:application
>>
>>
>> nginx
>> ~~~~~
>> The config for the domain: /etc/nginx/sites-enabled/www.example.com
>>  http://paste.pocoo.org/show/347762/
>>
>> The configuration of nginx /etc/nginx/nginx.conf
>>  http://paste.pocoo.org/show/347763/
>>
>> The problem is with the "redirect" function and the url_for.
>>
>> In fact, in my flask app, if I use redirect(url_for('index')), the URL 
will be the "bind" address of gunicorn, in this case: 127.0.0.1:18000 and 
I get this url in my browser.
>> but not with _external=True argument of the url_for function.
>>
>> I want to know where the problem is and if it's in the config of 
gunicorn or nginx, I have a doubt with my nginx conf, but I'm not sure.
>>
>> Could you help me ?
>>
>> Regards,
>>
>> Stéphane
>>
>>
>>
>>
>>

Re: [flask] Problem with Nginx + gunicorn + flask

From:
Sebastien Estienne
Date:
2011-03-04 @ 00:43
Hello

have you read this:
http://flask.pocoo.org/docs/deploying/others/#proxy-setups

But i don't think i need this in my setup of gunicorn + nginx.

Have you tried proxy_redirect off in nginx?

On a side note, your statics should be handle directly by nginx bypassing flask

Sebastien Estienne



On Fri, Mar 4, 2011 at 01:22, Stephane Wirtel <stephane@wirtel.be> wrote:
> Hi all,
>
> I want to use nginx with gunicorn and flask but I have a problem when I 
try to use the redirect function, I don't have the real host.
>
> As you know, I use gunicorn for the flask application and nginx is the 
web-server for the cache, etc...
>
> Internet <-> Nginx <-> Gunicorn <-> Flask
>                   <-> Static Files
>
> Here is my gunicorn config file
>
> gunicorn.conf
> ~~~~~~~~~~~~~
> workers = 2
> bind='127.0.0.1:18000'
> proc_name = 'www.example.com'
> pidfile='/tmp/www.example.com.pid'
>
> deploy.py
> ~~~~~~~~~
> from main import create_app
> from main import ProductionConfig
>
> __all__ = ['application']
>
> application = create_app(ProductionConfig())
>
> The command line to execute the gunicorn server
>
> $> gunicorn --log-level=debug -c gunicorn.conf deploy:application
>
>
> nginx
> ~~~~~
> The config for the domain: /etc/nginx/sites-enabled/www.example.com
>  http://paste.pocoo.org/show/347762/
>
> The configuration of nginx /etc/nginx/nginx.conf
>  http://paste.pocoo.org/show/347763/
>
> The problem is with the "redirect" function and the url_for.
>
> In fact, in my flask app, if I use redirect(url_for('index')), the URL 
will be the "bind" address of gunicorn, in this case: 127.0.0.1:18000 and 
I get this url in my browser.
> but not with _external=True argument of the url_for function.
>
> I want to know where the problem is and if it's in the config of 
gunicorn or nginx, I have a doubt with my nginx conf, but I'm not sure.
>
> Could you help me ?
>
> Regards,
>
> Stéphane
>
>
>
>
>