python - 使用 Nginx 在树莓派上部署 Flask

标签 python nginx flask raspberry-pi gunicorn

我正在尝试使用 nginx 在我的树莓派上部署一个简单的 flask 应用程序。我遵循了这两个指南:

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

并且让一切运行无误。但是当我加载一个指向我的 PI 的 IP 的网络浏览器时(我通过 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(符号链接(symbolic link)到:/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 上运行它可能不是最好的主意,但到目前为止我学到了很多东西。

最佳答案

您可能会在默认端口 5000 上运行 flask。

尝试改变这一行:

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

关于python - 使用 Nginx 在树莓派上部署 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20911516/

相关文章:

python - pandas to_json 将日期格式更改为 "epoch time"

javascript - Flask:提交表单时为什么不调用 AJAX?

python - Pandas 日期偏移和转换

python - 用于构建 TCP 服务器的良好 Python 网络库?

Python (PyRRD) 和带有 while 循环的 RRD?

php - Nginx + php-fpm : Bad gateway only when xdebug server is running

Django项目部署: cannot load static files

python - Firestore Python 中的完成处理

python:缺少1个必需的位置参数: 'self'

tomcat - 如何将域名映射到 Tomcat 应用程序?