【问题标题】:Run Python Flask application with nginx Unit使用 nginx 单元运行 Python Flask 应用程序
【发布时间】:2018-06-17 18:58:33
【问题描述】:

我今天玩了 nginx Unit 应用程序服务器和 Python 3。只要我运行一个简单的 WSGI 应用程序,它就可以正常工作,但是在尝试使用 Flask 时我不会工作 (404)...

nginx 配置:

upstream unit_backend {
    server 127.0.0.1:8300;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    server_name _;

    location / {

    }

    location /unittest {
            proxy_pass http://unit_backend;
            proxy_set_header Host $host;
    }
}

单元配置:

{
"listeners": {
    "*:8300": {
        "application": "test"
    }
},

"applications": {
    "test": {
        "type": "python 3.5",
        "processes": 4,
        "user": "noisefloor",
        "group": "noisefloor",
        "path": "/home/noisefloor/code/unit_test",
        "module": "wsgi"
    }
},

"access_log": "/var/log/access.log"
}

wsgi.py 在浏览器中打开http://127.0.0.1/unittest 时起作用:

from datetime import datetime

def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]
    start_response(status, headers)
    text = 'Hello World @ {}'.format(datetime.now())
    return [text.encode('utf-8')]

当wsgi.py如下:

from flask import Flask

    application = Flask(__name__)

    @application.route('/')
    def test():
        return 'Hello Flask!'

它得到了 404,但我看到了调用 http://127.0.0.1:8300 时我期望看到的内容 - 所以它基本上可以工作,只是不明白为什么调用 http://127.0.0.1/unittest 时它不能工作

【问题讨论】:

    标签: python nginx flask


    【解决方案1】:

    这个问题很老,但我相信它值得回答(如果你输入 Nginx Unit,它是谷歌第一个索引的问题)。

    由于您没有在 proxy_pass 指令中指定任何 URI 请求使用/unittest URI 传递给您的烧瓶应用程序,而您 显然除了烧瓶应用程序中没有指定任何路线 "/"

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2021-10-18
      • 2021-01-01
      • 2022-01-15
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多