我是一个初学者,我将要运行我的第一个Flask应用程序。我必须用Nginx和uWSGI部署一个Flask应用程序。这个应用程序在uWSGI上运行,但不在Nginx中。我将请你看看我下面提到的所有尝试。代码如下:
尝试1
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
if __name__ == "__main__":
app.run(host='0.0.0.0')
wsgi.py
from api import app
if __name__ == "__main__":
app.run()
(我被指示将其放在nginx.conf的虚拟主机配置中)
server {
listen 8000;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
}
location /Hello {
alias /project/challenges/api.py; (I doubt this)
}
} uwsgi.ini文件
[uwsgi]
module = wsgi:app
master = true
processes = 5
问题中给出的用于测试我的代码的测试代码如下:tests.py
import os
import pytest
import requests
from .api import app
class Test_API:
client = app.test_client()
def test_hello_from_app(self):
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):
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):
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
assert "listen 8000" in content
错误包括:(404状态)
=================================== short test summary info ====================================
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...
不允许我编辑tests.py文件。该文件已写入且为只读。
我怀疑deploy. conf中的location /Hello{}块。
目录结构为:
projects
|__challenge
|_ __init__.py
|__api.py
|__deploy.conf
|__wsgi.py
|__uwsgi.ini
|__ tests.py
尝试2:
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;
}
}
其余保持不变。
出现相同错误
错误包括:(404状态)
=================================== short test summary info ====================================
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...
尝试3:
deploy.conf:
服务器{侦听8000;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
}
location /Hello/ {
alias http://localhost:8000/Hello/;
}
}
其余保持不变。
错误包括:(502状态)
=================================== short test summary info ====================================
FAILED tests.py::Test_API::test_hello_from_nginx_server - assert 502 == 200
FAILED tests.py::Test_API::test_conf_file_contents - AssertionError: assert 'server localhost...
尝试4:
deploy.conf:
server {
listen 8000;
server_name localhost:8081;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
}
location /Hello/ {
alias http://localhost:8000/Hello/;
}
}
其余保持不变。
出现相同错误。
错误包括:(502状态)
=================================== short test summary info ====================================
FAILED tests.py::Test_API::test_hello_from_nginx_server - assert 502 == 200
FAILED tests.py::Test_API::test_conf_file_contents - AssertionError: assert 'server localhost...
In all the cases 1 error was common i.e Assertion error of "server localhost:8081".
我提到的test.py文件实际上是测试我的代码,上面。这个文件已经提供了问题,我不应该编辑它。
Nginx服务器启动正常。但我得到这些错误。
我怀疑位置/Hello{}区块。
请告诉我该怎么做。
尝试5
部署.conf:
server {
listen 8000;
location /Hello {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
}
}
uwsgi.ini
[uwsgi]
socket = 127.0.0.1:8081
module = wsgi:app
master = true
processes = 5
错误:
FAILED tests.py::Test_API::test_hello_from_nginx_server - assert 502 == 200
FAILED tests.py::Test_API::test_conf_file_contents - AssertionError: assert 'server localhost:8081'..
尝试6
deploy.conf
server {
listen 8000;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8081;
}
location /Hello {
alias http://localhost:8000;
}
}
uwsgi.ini
[uwsgi]
socket = 127.0.0.1:8081
module = wsgi:app
master = true
processes = 5
型
错误:
====================================== short test summary info =======================================
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:8081'..
1条答案
按热度按时间nhaq1z211#
这可以用另一种方式来完成。
在
deploy.conf
中执行以下更改:在ngindx.conf中添加deploy.conf。
然后单击Run。它将使用以下命令启动gunicorn服务器:
现在手动打开另一个终端并运行以下命令:
现在你的测试用例可能会通过,我想。试着让我知道,如果这工作与否。