使用Nginx在Raspberry Pi上部署Flask

时间:2014-01-03 19:43:53

标签: python nginx flask raspberry-pi gunicorn

我正在尝试使用nginx在我的覆盆子pi上部署一个简单的烧瓶应用程序。我遵循了这两个指南:

http://www.onurguzel.com/how-to-run-flask-applications-with-nginx-using-gunicorn/ http://www.onurguzel.com/managing-gunicorn-processes-with-supervisor/

并且一切运行都没有错误。但是,当我加载指向我的PI的IP的Web浏览器(我在ssh上工作)时 - 我看到的只是默认的“欢迎使用nginx”页面。发生了什么事?

这是我的档案:

/home/pi/hello/hello.py

from flask import Flask
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello world!"

app.wsgi_app = ProxyFix(app.wsgi_app)

if __name__ == '__main__':
    app.run()

/etc/nginx/sites-available/hello.conf (符号链接到: / etc / nginx / sites-enabled /

server {
    listen 80;
    server_name hello.itu24.com;

    root /home/pi/hello/hello.py;

    access_log /home/pi/hello/access.log;
    error_log /home/pi/hello/error.log;

    location / {
        try_files $uri @gunicorn_proxy;
        }

    location @gunicorn_proxy {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8000;
        }
}    

这是我的nginx.conf(虽然我根本没有改变它)

/etc/nginx/nginx.conf

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

对于主管部分:

/etc/supervisor/conf.d/hello.conf

[program:hello]
command = /home/pi/hello/bin/python /home/pi/hello/bin/gunicorn hello:app
directory = /home/pi/hello
user = pi

我可以用以下方式解决所有问题:

sudo supervisorctl start hello

但是当我点击我的Pi的IP时:

http://192.168.1.28 

来自我的Mac浏览器 我得到的是:“欢迎来到nginx”

有什么想法吗?这是我运行和部署的第一台服务器 - 在Ras Pi上运行它可能不是最好的想法,但到目前为止我学到了很多东西。

2 个答案:

答案 0 :(得分:2)

您可以在默认端口上运行flask,即5000。

尝试更改此行:

if __name__ == '__main__':
    #app.run()
    app.run(port=8000)

或将您的supervisord命令更改为:

command = /home/pi/hello/bin/python /home/pi/hello/bin/gunicorn hello:app -b 0.0.0.0:8000

答案 1 :(得分:2)

您可能希望确保禁用默认网站。只需从default删除符号链接sites-enabled

此外,Flask的默认端口是5000而不是8000,因此在nginx配置中,您需要更改以下内容:

location @gunicorn_proxy {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:5000; # Default port
}