繁体   English   中英

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

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

在将Flask作为实时网络服务器运行了几天之后,我知道这不是一件明智的事情:经过数小时的不活动之后,服务器死了,即使我使用最简单的设置(一页,没有python代码)也是如此。 在谷歌上搜索时,我发现Flask并不是要用作生产服务器,而好的做法是将其与Nginx和uWSGI结合使用。 因此,我遵循了本指南 ,它似乎运行良好。 但是现在我不知道如何重新启动,刷新或重新加载发布到内容或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;
 }
}

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

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

解决方案1

常见方法之一是在uwsgi_conf.ini添加以下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

uwsgi是承载应用程序的部分,因此如果应用程序发生更改,它是需要重新启动的部分。

对于自动重新加载,您可以尝试以下答案: https : //stackoverflow.com/a/41529718/187292

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM