【问题标题】:How to handle web api by flask如何通过flask处理web api
【发布时间】:2014-12-08 03:04:52
【问题描述】:

我正在使用 Nginx+uWSGI+Flask 来构建 Web 服务 API。

我关注http://flask.pocoo.org/docs/0.10/deploying/uwsgi/如下

在 Nginx 中,我希望 Flask 处理所有请求 appapi,而其他请求由 nginx 处理。

ex.
http://www.example.com/appapi/query?name=123 将由 flask 处理
http://www.example.com/ 将由 nginx 处理。

我添加下面的配置让烧瓶处理。

location = /appapi { rewrite ^ /appapi /; }
location /appapi { try_files $uri @appapi ; }
location @appapi {
  include uwsgi_params;
  uwsgi_param SCRIPT_NAME /appapi;
  uwsgi_modifier1 30;
  uwsgi_pass 127.0.0.1:3301;
}

uWSGI 有监听 3301 端口,会在 Flask 应用代码中加载烧瓶应用。我已经为 appapi 定义了路由

@app.route('/appapi/query', methods=['GET'])
def query():
    print 'query()'

但是我发现查询函数没有被调用,并且在日志中。它返回 404,未找到。
提前致谢!

【问题讨论】:

    标签: python nginx flask


    【解决方案1】:

    你可以这样做:

    @app.route('/query', methods=['GET'])
    def query():
        print 'query()'
    

    然后在 Nginx 配置中:

    location /appapi/ {
      include uwsgi_params;
      uwsgi_pass 127.0.0.1:3301;
    }
    

    【讨论】:

    • 它似乎有效,谢谢,我还有一个问题,为什么文档 (flask.pocoo.org/docs/0.10/deploying/uwsgi) 使用 uwsgi_modifier1 和 uwsgi_param 以及为什么它不起作用?
    • uwsgi_modifier1 已弃用。 uwsgi_param 传递脚本名称,但仅使用基于 / 的路由更简单
    • 是的,我使用/路由,它工作正常,然后我只想将api路由到flask,其他人由nginx处理,但是我遇到了post中提出的问题。
    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2017-10-11
    • 2021-03-12
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多