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

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

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

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

卷曲API端点在ec2框内起作用

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

现在,我通过提供系统服务将这种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.route('/api/v1/trump/rant/')
def return_rant():
    foo = # logic
    return jsonify(rant=foo)

解决方法:

您是否做了nginx -s reload&& systemctl重启nginx?

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

--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来检查传入请求的主机头,它不是私有地址. (您通常使用域名或URL栏中的公共地址访问)

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

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

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-Heroku,Django,工头


下一篇:linux-gunicorn在启动后无法启动