hikerjobs
hikerjobs

Reputation: 386

nginx + gunicorn + flask - 502 and 404 error on EC2

I have a setup on Amazon EC2 consisting of nginx + gunicorn + flask. The Flask program is supposed to be serving up REST APIs. But when I try to access the URL, I get a 502 and 404 error. Tried several things based on many related issues on StackOverflow and elsewhere but no luck. Hoping someone can help.

This is what my setup looks like:

nginx:

File is named: /etc/nginx/sites-enabled/abcbackend

server {
    listen 80;
    server_name abc.xxx.yyy.com;

    location /app1/ {
        include proxy_params;
        proxy_pass http://localhost:3000;
        proxy_ssl_name abc.xxx.yyy.com;
        proxy_ssl_server_name on;
    }
}

gunicorn

File is in /etc/systemd/system/abc.service

[Unit]
Description=Gunicorn instance to serve ABC backend service REST API
After=network.target

[Service]
User=tomtom
Group=www-data
WorkingDirectory=/home/tomtom/ABC/Flask
Environment="PATH=/home/tomtom/ABC/env/bin"
ExecStart=/home/tomtom/ABC/env/bin/gunicorn --workers 3 --bind 0.0.0.0:3000 --access-logfile /var/log/abc/gunicorn-access.log --error-logfile /var/log/abc/gunicorn-error.log wsgi:app

[Install]
WantedBy=multi-user.target

Flask

The Flask app is in the /home/tomtom/ABC/Flask directory and has two files:

wsgi.py

from appServer import app

if __name__ == "__main__":
    app.run()

appServer.py

from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/getDummyData', methods=['GET','POST'])
def get_dummy_data():
    return 'Dummy data from Hello World!'

@app.route('/', methods=['GET','POST'])
def hello_world():
    return 'Hello World! This is from the Python Flask server'

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Tried various combinations of 0.0.0.0, 127.0.0.1, and localhost in the above files but none of them work.

I try to go to this URL "http://abc.xxx.yyy.com/app1" and it gives me a "Requested URL was not found on the server" and if I try "http://abc.xxx.yyy.com/getDummyData" I get a 404 Not Found error.

If I log in directly to the EC2 and run a curl command, it works successfully: curl -X GET http://0.0.0.0:3000/ and curl -X GET http://0.0.0.0:3000/getDummyData both work.

Just going to http://abc.xxx.yyy.com in my browser also works and gives me a "Welcome to nginx" message.

What could be going wrong?

EDIT::

Added nginx.conf:

user tomtom;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
                          

Upvotes: 2

Views: 1431

Answers (1)

hikerjobs
hikerjobs

Reputation: 386

So the problem was that this nginx line:

proxy_pass http://localhost:3000;

should have been:

proxy_pass http://localhost:3000/;

That's it. Just that single "/". Made all the difference. :-) Closing this issue now. Thanks everyone.

Upvotes: 0

Related Questions