在ubuntu13.10下使用gunicorn部署flask程序

其實我使用的linuxmint16 ,不過他是基於ubuntu13.10的,按照從網上找到的一片博文操做,基本ok,不過仍是除了一些小問題,記錄一下:python

博文地址:How to Run Flask Applications with Nginx Using Gunicornlinux

我沒用virtualenvnginx

第一步:安裝gunicornflask

sudo pip install gunicorn
#也能夠
sudo easy_install gunicorn

第二步:ubuntu


app

app.run()

的前面加上:spa

from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)

運行它:
調試

gunicorn hello:app

這個hello就是code

app.run()

所在文件的名字,至於冒號後面的app天然就是app.run()的app了。server

其實在文檔裏還有這樣一種運行方法,不過我沒成功:

gunicorn -w 4 -b 127.0.0.1:4000 myproject:app

第三步,安裝、配置nginx。

 sudo apt-get install nginx
 sudo gedit /etc/nginx/sites-available/default

server {
    listen 80;
    server_name localhost;
 
    root /home/ranvane/develop/cdlist_flask;
    
    access_log /home/ranvane/develop/cdlist_flask/logs/access.log;
    error_log /home/ranvane/develop/cdlist_flask/logs/error.log;
 
    location / {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:5000;
            break;
        }
    }
}

修改完用

sudo nginx -t

 檢查一下配置文件是否正確。

/home/ranvane/develop/cdlist_flask是有app.run()文件的目錄,proxy_pass http://127.0.0.1:5000;是端口轉發,這裏我就遇到了問題,其實gunicorn hello:app是轉發到了8000端口,可是nginx就是不能解析,我看這控制檯顯示的仍是調試時顯示的「* Running on http://127.0.0.1:5000/」,因而就試着把端口改成5000,就成了。


這其實就是我遇到的問題:

gunicorn運行時綁定的端口nginx不會轉發。。。。。。。。。

唉,先用着吧。。。等會再查查。


嘿嘿,找到問題了,只要將 app.run()  註釋掉就能夠了。

相關文章
相關標籤/搜索