librelist archives

« back to archive

Problem Deploying Flask with Gunicorn + Nginx

Problem Deploying Flask with Gunicorn + Nginx

From:
Cory Li
Date:
2011-03-25 @ 09:00
Hello,

I am attempting to use Flask+Gunicorn+Nginx, and following exactly the 
example provided by http://wirtel.be/2011/02/24/nginx_gunicorn_flask/ 
exactly, but I'm still having issues.
Gunicorn throws the following error, and I'm not really sure what the 
problem is.  Any assistance would be appreciated!

2011-03-25 04:46:55 [5868] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/gunicorn/workers/sync.py", 
line 96, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 889, in
__call__
    return self.wsgi_app(environ, start_response)
  File 
"/usr/local/lib/python2.6/dist-packages/werkzeug/contrib/fixers.py", line 
106, in __call__
    return self.app(environ, start_response)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 871, in
wsgi_app
    with self.request_context(environ):
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 836, in
request_context
    return _RequestContext(self, environ)
  File "/usr/local/lib/python2.6/dist-packages/flask/ctx.py", line 32, in __init__
    self.url_adapter = app.create_url_adapter(self.request)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 755, in
create_url_adapter
    server_name=self.config['SERVER_NAME'])
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/routing.py", line 
1073, in bind_to_environ
    server_name += ':' + environ['SERVER_PORT']
TypeError: cannot concatenate 'str' and 'int' objects

~Cory

Re: [flask] Problem Deploying Flask with Gunicorn + Nginx

From:
Stephane Wirtel
Date:
2011-03-25 @ 16:40
Hi Cory,

Thank for the link to my article.

So, there is a solution, when you use nginx + gunicorn, your Flask app 
doesn't know there is a Proxy, so, there is a fix from Werkzeug.

[code]
from project import create_app
from werkzeug.contrib.fixers import ProxyFix

__all__ = ['application']

application = create_app(ProductionConfig())
application.wsgi_app = ProxyFix(application.wsgi_app)
[/code]

I will update my article.

There is an explanation at 
http://flask.pocoo.org/docs/deploying/others/?highlight=proxyfix#proxy-setups

Regards,

Stéphane

On 03/25/2011 10:00 AM, Cory Li wrote:
> Hello,
> 
>  
> 
> I am attempting to use Flask+Gunicorn+Nginx, and following exactly the 
example provided by http://wirtel.be/2011/02/24/nginx_gunicorn_flask/ 
exactly, but I’m still having issues.
> 
> Gunicorn throws the following error, and I’m not really sure what the 
problem is.  Any assistance would be appreciated!
> 
>  
> 
> 2011-03-25 04:46:55 [5868] [ERROR] Error handling request
> 
> Traceback (most recent call last):
> 
>   File 
"/usr/local/lib/python2.6/dist-packages/gunicorn/workers/sync.py", line 
96, in handle_request
> 
>     respiter = self.wsgi(environ, resp.start_response)
> 
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 889, 
in __call__
> 
>     return self.wsgi_app(environ, start_response)
> 
>   File 
"/usr/local/lib/python2.6/dist-packages/werkzeug/contrib/fixers.py", line 
106, in __call__
> 
>     return self.app(environ, start_response)
> 
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 871, 
in wsgi_app
> 
>     with self.request_context(environ):
> 
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 836, 
in request_context
> 
>     return _RequestContext(self, en viron)
> 
>   File "/usr/local/lib/python2.6/dist-packages/flask/ctx.py", line 32, 
in __init__
> 
>     self.url_adapter = app.create_url_adapter(self.request)
> 
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 755, 
in create_url_adapter
> 
>     server_name=self.config['SERVER_NAME'])
> 
>   File "/usr/local/lib/python2.6/dist-packages/werkzeug/routing.py", 
line 1073, in bind_to_environ
> 
>     server_name += ':' + environ['SERVER_PORT']
> 
> TypeError: cannot concatenate 'str' and 'int' objects
> 
>  
> 
> ~Cory
> 
>  
> 

Re: [flask] Problem Deploying Flask with Gunicorn + Nginx

From:
bruce bushby
Date:
2011-03-25 @ 09:14
a wild guess here....have you declared your port as an int rather then a
string ?  Perhaps try "port = "123" ...rather then port = 123


On Fri, Mar 25, 2011 at 9:00 AM, Cory Li <coryli@mit.edu> wrote:

> Hello,
>
>
>
> I am attempting to use Flask+Gunicorn+Nginx, and following exactly the
> example provided by 
http://wirtel.be/2011/02/24/nginx_gunicorn_flask/exactly, but I’m still 
having issues.
>
> Gunicorn throws the following error, and I’m not really sure what the
> problem is.  Any assistance would be appreciated!
>
>
>
> 2011-03-25 04:46:55 [5868] [ERROR] Error handling request
>
> Traceback (most recent call last):
>
>   File "/usr/local/lib/python2.6/dist-packages/gunicorn/workers/sync.py",
> line 96, in handle_request
>
>     respiter = self.wsgi(environ, resp.start_response)
>
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 889, in
> __call__
>
>     return self.wsgi_app(environ, start_response)
>
>   File "/usr/local/lib/python2.6/dist-packages/werkzeug/contrib/fixers.py",
> line 106, in __call__
>
>     return self.app(environ, start_response)
>
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 871, in
> wsgi_app
>
>     with self.request_context(environ):
>
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 836, in
> request_context
>
>     return _RequestContext(self, en viron)
>
>   File "/usr/local/lib/python2.6/dist-packages/flask/ctx.py", line 32, in
> __init__
>
>     self.url_adapter = app.create_url_adapter(self.request)
>
>   File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 755, in
> create_url_adapter
>
>     server_name=self.config['SERVER_NAME'])
>
>   File "/usr/local/lib/python2.6/dist-packages/werkzeug/routing.py", line
> 1073, in bind_to_environ
>
>     server_name += ':' + environ['SERVER_PORT']
>
> TypeError: cannot concatenate 'str' and 'int' objects
>
>
>
> ~Cory
>
>
>

Re: [flask] Problem Deploying Flask with Gunicorn + Nginx

From:
Cory Li
Date:
2011-03-25 @ 16:36
Where would I actually set the port configuration in this setup?



From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of bruce bushby
Sent: Friday, March 25, 2011 5:14 AM
To: flask@librelist.com
Subject: Re: [flask] Problem Deploying Flask with Gunicorn + Nginx


a wild guess here....have you declared your port as an int rather then a 
string ?  Perhaps try "port = "123" ...rather then port = 123

On Fri, Mar 25, 2011 at 9:00 AM, Cory Li 
<coryli@mit.edu<mailto:coryli@mit.edu>> wrote:
Hello,

I am attempting to use Flask+Gunicorn+Nginx, and following exactly the 
example provided by http://wirtel.be/2011/02/24/nginx_gunicorn_flask/ 
exactly, but I’m still having issues.
Gunicorn throws the following error, and I’m not really sure what the 
problem is.  Any assistance would be appreciated!

2011-03-25 04:46:55 [5868] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/gunicorn/workers/sync.py", 
line 96, in handle_request

    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 889, in
__call__
    return self.wsgi_app(environ, start_response)
  File 
"/usr/local/lib/python2.6/dist-packages/werkzeug/contrib/fixers.py", line 
106, in __call__
    return self.app(environ, start_response)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 871, in
wsgi_app
    with self.request_context(environ):
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 836, in
request_context
    return _RequestContext(self, en viron)
  File "/usr/local/lib/python2.6/dist-packages/flask/ctx.py", line 32, in __init__
    self.url_adapter = app.create_url_adapter(self.request)
  File "/usr/local/lib/python2.6/dist-packages/flask/app.py", line 755, in
create_url_adapter
    server_name=self.config['SERVER_NAME'])
  File "/usr/local/lib/python2.6/dist-packages/werkzeug/routing.py", line 
1073, in bind_to_environ
    server_name += ':' + environ['SERVER_PORT']
TypeError: cannot concatenate 'str' and 'int' objects

~Cory