如何用flask和nginx启动uwsgi
How to start uwsgi with flask and nginx
我想我的主要问题是我不知道文件层次结构应该是什么样子?到目前为止,我一直在关注 Grinberg 在他的 "Flask development" 书中的教程。所以我喜欢:
--manage.py ( Flask's Script extension script)
--app/ ( application folder as a package)
--virtual_env
不确定我搞砸了什么,但现在当我尝试使用 uwsgi 命令时,它显示以下错误:
current working directory: /home/gaucan/temp/my_app
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
编辑:
像这样开始工作:
uwsgi --http :9090 -w manage:app --enable-threads
这行得通……在 manage.py 我有一行:
app=create_app('default')
所以这几乎就是我想我需要做的...
但我仍然无法摆脱上面的警告...我 运行 uwsgi 没有它的主进程管理器...可以吗?还是我做错了什么?
这是刚刚创建的 /etc/nginx/nginx.conf 文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream uwsgicluster {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to by-pass for static files
location ^~ /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /app/static/;
}
# Serve a static file (ex. favico) outside static dir.
location = /favico.ico {
root /app/favico.ico;
}
# Proxying connections to application servers
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
这可能与您安装 uwsgi 的方式有关。此警告:
!!! no internal routing support, rebuild with pcre support !!!
与您的应用程序无关,它与您的 uwsgi 二进制文件有关。
基本上它表示您正在使用的二进制文件中没有启用 uwsgi 的一部分。 运行 您的 Flask 应用程序不需要该特定功能,因此您可以忽略该警告。但如果您想了解更多信息,请参阅 this question 以获取有关此问题及其解决方法的一些信息。
现在,关于另一个警告:
*** WARNING: you are running uWSGI without its master process manager ***
我认为您缺少 --master
选项来启用 prefork 服务器。
我想我的主要问题是我不知道文件层次结构应该是什么样子?到目前为止,我一直在关注 Grinberg 在他的 "Flask development" 书中的教程。所以我喜欢:
--manage.py ( Flask's Script extension script)
--app/ ( application folder as a package)
--virtual_env
不确定我搞砸了什么,但现在当我尝试使用 uwsgi 命令时,它显示以下错误:
current working directory: /home/gaucan/temp/my_app
detected binary path: /usr/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
编辑: 像这样开始工作:
uwsgi --http :9090 -w manage:app --enable-threads
这行得通……在 manage.py 我有一行:
app=create_app('default')
所以这几乎就是我想我需要做的...
但我仍然无法摆脱上面的警告...我 运行 uwsgi 没有它的主进程管理器...可以吗?还是我做错了什么?
这是刚刚创建的 /etc/nginx/nginx.conf 文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream uwsgicluster {
server 127.0.0.1:8080;
# server 127.0.0.1:8081;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to by-pass for static files
location ^~ /static/ {
# Example:
# root /full/path/to/application/static/file/dir;
root /app/static/;
}
# Serve a static file (ex. favico) outside static dir.
location = /favico.ico {
root /app/favico.ico;
}
# Proxying connections to application servers
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
这可能与您安装 uwsgi 的方式有关。此警告:
!!! no internal routing support, rebuild with pcre support !!!
与您的应用程序无关,它与您的 uwsgi 二进制文件有关。
基本上它表示您正在使用的二进制文件中没有启用 uwsgi 的一部分。 运行 您的 Flask 应用程序不需要该特定功能,因此您可以忽略该警告。但如果您想了解更多信息,请参阅 this question 以获取有关此问题及其解决方法的一些信息。
现在,关于另一个警告:
*** WARNING: you are running uWSGI without its master process manager ***
我认为您缺少 --master
选项来启用 prefork 服务器。