1、测试环境配置如下:
虚拟机:Ubuntu16.04 4G内存 双核处理器
运行环境python+flask+gunicorn+nginx
flask 、nginx和 gunicorn 安装方法都非常简单粗暴:sudo apt-get install 就好
附上一个安装教程点击打开链接
这里我们为了使用一个域名来代替访问默认的127.0.0.1:8080,将配置nginx如下:
在/etc/nginx/conf.d 下新建文件flask_nginx.conf
添加如下内容:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
server {
listen 80;
server_name www.flasktest.com;
location / {
proxy_pass http://127.0.0.1:8080;
access_log /home/api_access.log;
proxy_read_timeout 300;
}
}
在/etc/hosts文件中新增一行 127.0.0.1 www.flasktest.com
通过如上配置,我们就可以通过访问www.flasktest.com域名来访问127.0.0.1:8080端口下的内容啦~
2、具体代码:
from