首页 > 解决方案 > Nginx+Flask如何解释python源文件?

问题描述

我正在尝试更新一个基于 nginx+Flask+uwsgi 的 Web 项目。当我更新任何 python 文件时,我发现 nginx 仍在使用旧文件。当我删除所有 *.pyc 文件时,python 解释器没有生成新的 pyc 文件。好像有缓存,我按照this question的回答尝试清除nginx的缓存。但它没有用。

有人知道让 nginx 从新源文件解释 python 的任何解决方案吗?

这是 nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

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

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen 80;
        server_name m.xxxx.com.cn;
        location / {
            include uwsgi_params;
            uwsgi_pass unix:/root/003_exampleproject/exampleproject.sock;
        }
    }

这是另一个配置文件:

(venv) [root@VM_0_3_centos 003_exampleproject]# cat /etc/systemd/system/exampleproject.service
[Unit]
Description=uWSGI instance to serve exampleproject
After=network.target

[Service]
User=root
Group=nginx
WorkingDirectory=/root/003_exampleproject/
Environment="PATH=/root/003_exampleproject/venv"
ExecStart=/root/003_exampleproject/venv/bin/uwsgi --ini exampleproject.ini --logto /var/log/uwsgi/exampleproject.log

[Install]
WantedBy=multi-user.target

(venv) [root@VM_0_3_centos 003_exampleproject]# cat exampleproject.ini
[uwsgi]
module = manage:app

master = true
processes = 5

socket = exampleproject.sock
chmod-socket = 660
vacuum = true

die-on-term = true
env = MBUS_ADMIN=root@example.com

标签: pythonnginxcachingflask

解决方案


简短的回答:

service exampleproject restart

或者

systemctl restart exampleproject.service

长答案:

关于需要重新启动的配置文件服务是:

exampleproject.service

如果你运行命令

service --status-all

你应该把它列在那里。

然后你可以使用命令:

service exampleproject 

将打印允许的命令。

那就试试吧:

service exampleproject restart

注意: service 命令应该可以在您的 centos 上运行,但如果在您的发行版中不起作用,您可以尝试使用替代方法:

systemctl restart exampleproject.service

推荐阅读