Flask application using gunicorn + nginx showing 404 [ec2]

I am trying to use a simple API following this digitalocean tutorial .

For testing purposes, I used to serve the API through gunicorn by doing

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

      

And twisting the API endpoint works inside the ec2 block

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

      

Now I have moved this firing process to start it on startup by making a systemd service

# /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

      

I created files

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

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

and changed owner and group to ubuntu

After enabling the service and rebooting, I checked the status

$ 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.

      

Following the DO tutorial, I tried to set up nginx incoming requests to the proxy on port 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;
    }
}

      

And then did

$ 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

      

Now if I try to make a GET

request to the API endpoint from outside the ec2 window

$ 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>

      

Same case when I try to do it from ec2 container too

$ 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>

      

log files

# /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$

      

EDIT

The relevant part of the flash application

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

      

+3


source to share


1 answer


Are you done nginx -s reload && systemctl restart nginx

?

Another thing you could try is to create bindings on the http port instead of the socket:

--bind 127.0.0.1:6767 #in systemd config

      

and change your nginx config like this:

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

      


And why do you have private_ip in nginx config?

server_name private_ip_address;

      

Change this to



server_name "_";
# OR
server_name PUBLIC_IP;

      

and remove all default config from /etc/nginx/site-enabled


1) using server_name private_ip_address ;?

Nginx uses servername to validate against the host header of the incoming request, and this is not a private address. (Usually you access using either the domain name or the public address in the URL bar)

2) I removed / etc / nginx / site -enabled / default dir to get everything working.

If your servername is incorrect, nginx processes the request using the default file or server block containing default_server

. So I asked you to delete this file in case there is a problem with your server name ^ _ ^

Also, what's the difference in API performance if I bind it to a port instead of a socket file as suggested in the blog?

This is generally a premature optimization, any difference you get will be exclusively within the margin of error compared to bottlenecks caused by flask / python and especially database connections. Though please take this with a grain of salt as I don't have a reliable source to quote this.

+3


source







All Articles