Nginx + Gunicorn +烧瓶-EC2上的502和404错误

时间:2020-10-05 06:22:23

标签: python-3.x nginx flask gunicorn

我在Amazon EC2上进行了设置,其中包括nginx + gunicorn + flask。 Flask程序应该提供REST API。但是,当我尝试访问URL时,出现502和404错误。基于StackOverflow和其他地方的许多相关问题尝试了几件事,但是没有运气。希望有人可以帮忙。

这是我的设置:

nginx:

文件名为:/ 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;
    }
}

独角兽

文件位于/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应用程序位于/ home / tomtom / ABC / Flask目录中,并且具有两个文件:

  • wsgi.py
  • appServer.py

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')

在上述文件中尝试了0.0.0.0、127.0.0.1和localhost的各种组合,但是它们都不起作用。

我尝试转到该URL“ http://abc.xxx.yyy.com/app1”,它给我一个“在服务器上找不到请求的URL”,如果我尝试“ http:// abc .xxx.yyy.com / getDummyData”,我收到404找不到错误。

如果我直接登录EC2并运行curl命令,它将成功工作: curl -X GET http://0.0.0.0:3000/和curl -X GET http://0.0.0.0:3000/getDummyData都可以。

只需在浏览器中转到http://abc.xxx.yyy.com,也会显示一条“欢迎使用nginx”消息。

出什么问题了?

编辑::

添加了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/*;
}
                          

1 个答案:

答案 0 :(得分:0)

所以问题在于这条nginx行:

proxy_pass http://localhost:3000;

应该是:

proxy_pass http://localhost:3000/;

就是这样。只是那个单一的“ /”。有所作为。 :-) 现在关闭此问题。 谢谢大家。