如何使用 flask/uwsgi/nginx 服务器管理内容和 python 代码?

how to manage content and python code with a flask/uwsgi/nginx server?

在 运行 Flask 作为实时网络服务器使用了几天后,我了解到这不是一件明智的事情:在几个小时不活动后服务器就死机了,即使我使用了最简单的设置(一页,没有 python 代码)。 谷歌搜索我发现 Flask 并不打算用作生产服务器,一个好的做法是将它与例如结合起来。 Nginx 和 uWSGI。所以我跟着 this guide 似乎 运行 不错。 但现在我不知道如何重新启动、刷新或重新加载发布我添加到内容或 python 代码中的内容所需的一切。我现在拥有三个引擎 运行,而不是在文件保存后(在调试模式下)自动重新加载的 Flask 服务器。终端重启nginx不行,我已经试过了

有人可以帮助这个菜鸟吗?

TIA!!

uwsgi_conf.ini:

==============================

[uwsgi]

chdir = /home/pi/sampleApp
module = sample_app:first_app

master = true
processes = 1
threads = 2

uid = www-data 
gid = www-data
socket = /tmp/sample_app.sock
chmod-socket = 664
vacuum = true

die-on-term = true

==============================

nginx.conf:

==============================

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

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;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # 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/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # 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;
#   }
#}

==============================

sample_app_proxy:

==============================

server {
 listen 80;
 server_name localhost;

 location / { try_files $uri @app; }
 location @app {
 include uwsgi_params;
 uwsgi_pass unix:/tmp/sample_app.sock;
 }
}

==============================

uwsgi 是托管您的应用程序的部分,因此如果应用程序更改,它是需要重新启动的部分。

对于自动重新加载,您可以尝试这个答案:

当与 Flask 一起使用时,Nginx 充当代理服务器,这意味着当您想要重新加载 Flask 应用程序时无需重新启动它。这是您需要照顾的 uWSGI,因为 uWSGI 位于 Flask 应用程序和 Nginx 之间,负责将所有请求转发给您的应用程序,而且 运行 它。

解决方案 1

一种常见的方法是在 uwsgi_conf.ini 中添加以下内容:

py-autoreload = 1

这将告诉 uWSGI 它需要每秒监控文件时间戳并在触发后重新加载应用程序。

解决方案 2

向uWSGI发送优雅的重新加载命令Master FIFO:

将以下内容添加到您的 uwsgi_conf.ini

master-fifo = /var/run/flask_uwsgi_fifo

然后在完成 Flask 源文件更改后重新加载 uWSGI:

$ echo r > /var/run/flask_uwsgi_fifo

解决方案 3

类似于解决方案 2,但通过 touch-reload.

将以下内容添加到您的 uwsgi_conf.ini

touch-reload = /var/run/flask_touch

然后通过以下方式重新加载您的应用:

$ touch /var/run/flask_touch

解决方案 4

发送SIGHUP到uWSGI pid文件。

将以下内容添加到您的 uwsgi_conf.ini

safe-pidfile = /tmp/flask.pid

然后通过以下方式重新加载您的应用:

$ kill -HUP `cat /tmp/flask.pid`

$ uwsgi --reload /tmp/flask.pid