【发布时间】:2021-01-14 02:14:28
【问题描述】:
我是第一次使用 Nginx 和 uWSGI 部署 Flask 应用程序。 Nginx 将监听 8000 端口,WSGI 将监听 8081。我在运行分配时出错。
问题陈述:用nginx和uwsgi部署一个简单的flask应用。
根据问题陈述配置 nginx 服务器的命令:
- sudo vi /etc/nginx/nginx.conf
- 我被要求在虚拟主机配置中包含 /projects/challenge/deploy.config。
- 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 文件,但我不知道如何解决这个问题。
请告诉我该怎么做。
【问题讨论】: