gpt4 book ai didi

python-3.x - 使用 gunicorn + nginx 服务 flask 应用程序显示 404 [ec2]

转载 作者:太空宇宙 更新时间:2023-11-03 16:55:56 25 4
gpt4 key购买 nike

我正在尝试通过遵循此 digitalocean tutorial 来提供一个简单的 API .

为了测试,我之前通过 gunicorn 提供 API 服务

$ gunicorn --bind 0.0.0.0:5000 trumporate.wsgi:app

并且在 ec2 框内 curl API 端点

$ curl -X GET http://0.0.0.0:5000/api/v1/trump/rant/
{
"foo": "bar"
}

现在我通过创建 systemd 服务将这个 gunicorn 进程转移到在启动时运行

# /etc/systemd/system/trumporate.service

[Unit]
Description=Gunicorn instance for trumporate
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/var/opt/trumporate
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunicorn-error.log trumporate.wsgi:app

[Install]
WantedBy=multi-user.target

我已经创建了文件

  • /var/log/trumporate/gunicorn-error.log
  • /var/log/trumporate/gunicorn-access.log

并将所有权和组更改为 ubuntu

开启服务并重启后,查看状态

$ sudo systemctl status trumporate.service
● trumporate.service - Gunicorn instance for trumporate
Loaded: loaded (/etc/systemd/system/trumporate.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2017-05-03 06:30:26 UTC; 1h 2min ago
Main PID: 1122 (gunicorn)
Tasks: 4
Memory: 92.2M
CPU: 1.390s
CGroup: /system.slice/trumporate.service
├─1122 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
├─1264 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
├─1266 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic
└─1267 /usr/bin/python3 /usr/local/bin/gunicorn --workers 3 --bind unix:trumporate.sock -m 007 --access-logfile /var/log/trumporate/gunicorn-access.log --error-logfile /var/log/trumporate/gunic

May 03 06:30:26 ip-172-31-25-173 systemd[1]: Started Gunicorn instance for trumporate.

按照 DO 教程,我尝试配置 nginx 以代理端口 80 上的传入请求

$ cat /etc/nginx/sites-available/trumporate
server {
listen 80;
server_name private_ip_address;

location / {
include proxy_params;
proxy_pass http://unix:/var/opt/trumporate/trumporate.sock;
}
}

然后做了一个

$ ln -s/etc/nginx/sites-available/trumporate/etc/nginx/sites-enabled

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

现在,如果我尝试从 ec2 框外对 API 端点执行 GET 请求

$ curl -X GET http://public_ip/api/v1/trump/rant
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>

当我尝试从 ec2 容器内部执行此操作时也是如此

$ curl -X GET http://localhost:80/api/v1/trump/rant/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>

日志文件

# /var/log/nginx/access.log
dev_box_ip - - [03/May/2017:05:50:45 +0000] "GET /api/v1/trump/rant/ HTTP/1.1" 404 580 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"
127.0.0.1 - - [03/May/2017:06:13:26 +0000] "GET /api/v1/trump/rant/ HTTP/1.1" 404 178 "-" "curl/7.47.0"
dev_box_ip - - [03/May/2017:07:42:42 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36"

/var/log/nginx$ cat error.log
/var/log/nginx$

/var/log/trumporate$ cat gunicorn-access.log
/var/log/trumporate$

$ cat gunicorn-error.log
[2017-05-03 06:28:41 +0000] [1884] [INFO] Starting gunicorn 19.7.1
[2017-05-03 06:28:41 +0000] [1884] [INFO] Listening at: unix:trumporate.sock (1884)
[2017-05-03 06:28:41 +0000] [1884] [INFO] Using worker: sync
[2017-05-03 06:28:41 +0000] [1889] [INFO] Booting worker with pid: 1889
[2017-05-03 06:28:41 +0000] [1890] [INFO] Booting worker with pid: 1890
[2017-05-03 06:28:41 +0000] [1891] [INFO] Booting worker with pid: 1891
[2017-05-03 06:29:48 +0000] [1884] [INFO] Handling signal: term
[2017-05-03 06:29:48 +0000] [1889] [INFO] Worker exiting (pid: 1889)
[2017-05-03 06:29:48 +0000] [1890] [INFO] Worker exiting (pid: 1890)
[2017-05-03 06:29:48 +0000] [1891] [INFO] Worker exiting (pid: 1891)
[2017-05-03 06:29:49 +0000] [1884] [INFO] Shutting down: Master
[2017-05-03 06:30:27 +0000] [1122] [INFO] Starting gunicorn 19.7.1
[2017-05-03 06:30:27 +0000] [1122] [INFO] Listening at: unix:trumporate.sock (1122)
[2017-05-03 06:30:27 +0000] [1122] [INFO] Using worker: sync
[2017-05-03 06:30:27 +0000] [1264] [INFO] Booting worker with pid: 1264
[2017-05-03 06:30:27 +0000] [1266] [INFO] Booting worker with pid: 1266
[2017-05-03 06:30:28 +0000] [1267] [INFO] Booting worker with pid: 1267
/var/log/trumporate$

编辑

flask app的相关部分

@app.route('/api/v1/trump/rant/')
def return_rant():
foo = # logic
return jsonify(rant=foo)

最佳答案

你做了nginx -s reload && systemctl restart nginx吗?

您可以尝试的另一件事是在 http 端口而不是套接字上进行绑定(bind):

--bind 127.0.0.1:6767 #in systemd config

并按如下方式更改 nginx 配置:

location / {
include proxy_params;
proxy_redirect off;
proxy_pass http://127.0.0.1:6767;
}

还有为什么你在 nginx 配置中有 private_ip?

server_name private_ip_address;

将其更改为

server_name "_";
# OR
server_name PUBLIC_IP;

并从 /etc/nginx/site-enabled 中删除所有默认配置


1) use of server_name private_ip_address;?

Nginx 使用 server_name 来检查传入请求的主机 header ,这不是私有(private)地址。 (通常使用域名或地址栏中的公共(public)地址访问)

2) I deleted /etc/nginx/site-enabled/default dir to make things to get to working.

如果您的 server_name 设置不正确,nginx 使用默认文件或包含 default_server 的服务器 block 处理请求。因此,我要求您删除该文件,以防万一您的服务器名称出现问题 ^_^

Also, what difference would it make to the performance of the API if I am binding it to a port instead of the socket file as suggested by the blog post?

这通常是过早的优化,与 flask/python 尤其是数据库连接造成的瓶颈相比,您获得的任何差异都纯粹在误差范围内。尽管请对此持保留态度,因为我没有任何可靠的来源来引用它。

关于python-3.x - 使用 gunicorn + nginx 服务 flask 应用程序显示 404 [ec2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com