【问题标题】:How to deploy Flask application with Nginx and uWSGI?如何使用 Nginx 和 uWSGI 部署 Flask 应用程序?
【发布时间】:2021-01-14 02:14:28
【问题描述】:

我是第一次使用 Nginx 和 uWSGI 部署 Flask 应用程序。 Nginx 将监听 8000 端口,WSGI 将监听 8081。我在运行分配时出错。

问题陈述:用nginx和uwsgi部署一个简单的flask应用。

根据问题陈述配置 nginx 服务器的命令:

  1. sudo vi /etc/nginx/nginx.conf
  2. 我被要求在虚拟主机配置中包含 /projects/challenge/deploy.config。
  3. sudo service nginx 重启

对于代码,这就是我所做的:wsgi.py

# Put your code here
from .api import app
if __name__ == "__main__":
    app.run()

deploy.conf

server {
    listen 8000;
    server_name localhost:8081;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8081;
    }

    location /Hello {
        alias /project/challenges/api.py;
    }
}

api.py

from flask import Flask, request, make_response


app = Flask(__name__)
app.secret_key = "Thisisyoursecret"


# Create a simple endpoint /Hello with return message "Welcome to your flask application"

@app.route('/Hello')
def hello():
  res=make_response("Welcome to your flask application")
  return res

根据问题中的说明,我将 deploy.conf 包含在 nginx.conf 文件的虚拟主机配置中,如下所示:

http{
   ...
        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        include /projects/challenge/deploy.conf;
}

uwsgi.ini

[uwsgi]
socket=127.0.0.1:8081
wsgi-file=wsgi.py

问题中给出的测试我的代码的测试代码是:

import os

import pytest
import requests
from .api import app



class Test_API:

client = app.test_client()

def test_hello_from_app(self): #Failed
    url = "/Hello"
    response = self.client.get(url)
    assert response.status_code == 200
    assert response.data == b'Welcome to your flask application'
        
def test_hello_from_app_on_port(self):   #Failed
    url = "http://localhost:8081/Hello"
    response = requests.get(url)
    assert response.status_code == 200
    assert response.text == 'Welcome to your flask application'

def test_hello_from_nginx_server(self):  #Failed
    url = "http://localhost:8000/Hello"
    response = requests.get(url)
    assert response.status_code == 200
    assert response.text == 'Welcome to your flask application'    


def test_conf_file_contents(self):
    with open('deploy.conf', 'r') as f:
        content = f.read()
        assert "location /Hello" in content
        assert "server localhost:8081" in content  # Assertion error here
        assert "listen 8000" in content

错误是:

            assert "location /Hello" in content
>           assert "server localhost:8081" in content
E           AssertionError: assert 'server localhost:8081' in 'server {\n    listen 8000;\n    server_name localhost:8081;\n\n    location / {\n        include uwsgi_params;\n        uwsgi_pass 127.0.0.1:8081;\n    }\n\n    location /Hello {\n        alias /project/challenges/api.py;\n    }\n}'

tests.py:34: AssertionError
======================================= warnings summary =======================================
/home/user/.local/lib/python3.5/site-packages/_pytest/junitxml.py:417
  /home/user/.local/lib/python3.5/site-packages/_pytest/junitxml.py:417: PytestDeprecationWarning: The 'junit_family' default value will change to 'xunit2' in pytest 6.0.
  Add 'junit_family=xunit1' to your pytest.ini file to keep the current format in future versions of pytest and silence this warning.
    _issue_warning_captured(deprecated.JUNIT_XML_DEFAULT_FAMILY, config.hook, 2)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
----------------------- generated xml file: /projects/challenge/unit.xml -----------------------
=================================== short test summary info ====================================
FAILED tests.py::Test_API::test_hello_from_app - assert 500 == 200
FAILED tests.py::Test_API::test_hello_from_app_on_port - assert 500 == 200
FAILED tests.py::Test_API::test_hello_from_nginx_server - assert 404 == 200
FAILED tests.py::Test_API::test_conf_file_contents - AssertionError: assert 'server localhost...
================================= 4 failed, 1 warning in 0.31s =================================   

Nginx 运行良好。 我浏览了 Google 和 Youtube,但不知道要解决什么问题。没有解决方案奏效。我是这个领域的初学者。我怀疑 deploy.conf 或 wsgi 文件,但我不知道如何解决这个问题。

请告诉我该怎么做。

【问题讨论】:

    标签: nginx flask


    【解决方案1】:

    您在nginx.conf 上忘记了;

    include /projects/challenge/deploy.conf

    应该是

    include /projects/challenge/deploy.conf;

    为防止将来发生这种情况,请始终使用nginx -t 来验证您的配置文件。

    【讨论】:

    • 我添加了分号。 nginx 现在正在运行。但是我在运行测试用例时遇到了错误。我正在相应地编辑我的问题。
    • 你的断言是错误的。 server_name localhost:8081; 这是您的 deploy.conf 文件中的值,而不是 server localhost:8081
    • 测试程序连同断言一起提供了问题本身,用于检查我的代码。我没有写断言。
    猜你喜欢
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2013-07-16
    • 2021-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多