Nginx 502 Bad Gateway after deploying Flask API to Elastic Beanstalk |111: Connection refused while connecting to upstream

I’m really new into servers and stepping from development into production and lately I’ve had some errors while deploying a Flask API to AWS Elastic Beanstalk. I “successfully” deployed my API through EB CLI as it said so, but when I open the domain, it only says “502 Bad Gateway – nginx”.

I downloaded the logs, and I’m pretty concerned about what nginx’s error.log says:

connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.41.209, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "172.31.22.111"

I thought the proccess was pretty straightforward when it comes to deploying to EB, but I’ve tried to solve this problem and I haven’t been able. My API runs perfectly in development; I’ve tested it a lot of times in localhost at default port, but now in deployment it no longer works. So I think it’s more of a reverse-proxy-related problem, but I don’t know much about this, so I’d appreciate so much your help. I’ve used Windows the whole time and virtually every info out there is Linux-related, so I’m very limited to it.

I’m going to paste a fragment of web.stdout.log, which looks like to show some errors related to Gunicorn:

Apr 27 23:44:18 ip-172-31-22-111 web[2165]: [2024-04-27 23:44:18 +0000] [2165] [INFO] Starting gunicorn 22.0.0

Apr 27 23:44:18 ip-172-31-22-111 web[2165]: [2024-04-27 23:44:18 +0000] [2165] [INFO] Listening at: http://127.0.0.1:8000 (2165)

Apr 27 23:44:18 ip-172-31-22-111 web[2165]: [2024-04-27 23:44:18 +0000] [2165] [INFO] Using worker: gthread

Apr 27 23:44:18 ip-172-31-22-111 web[2197]: [2024-04-27 23:44:18 +0000] [2197] [INFO] Booting worker with pid: 2197

Apr 27 23:44:18 ip-172-31-22-111 web[2197]: [2024-04-27 23:44:18 +0000] [2197] [ERROR] Exception in worker process

Apr 27 23:44:18 ip-172-31-22-111 web[2197]: Traceback (most recent call last): Apr 27 23:44:18 ip-172-31-22-111 web[2197]: File "/var/app/venv/staging-LQM1lest/lib64/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker

Please help!

This is the Python Flask API I’ve coded and tested successfully:


import json
from flask_cors import CORS
from flask import (Flask, Response, request)
import util


application = Flask(__name__)
CORS(application)  
RESPONSE_HEADERS = { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }    

@application.route('/welcome')
def welcome():
    valA, valB = util.get_years(-1, -1, 1)
    message = "This is a test for the API and the underlying function. Val A: " + str(valA) + ", Val B: " + str(valB)
    return message

@application.route('/years2pay', methods=['POST'])
def calc():
    data = request.get_json()
    strategy_pA = data.get('strategy_pA', None)
    strategy_pB = data.get('strategy_pB', None)
    w = data.get('w', None)
    
    if not strategy_pA or not strategy_pB or w == None:
        return Response(json.dumps({ 'error': 'Not enough data' }), status=400, headers=RESPONSE_HEADERS)
    
    valA, valB = util.get_years(int(strategy_pA), int(strategy_pB), float(w))
    return Response(json.dumps({ 'years_playerA': valA, 'years_playerB': valB }), status=200, headers=RESPONSE_HEADERS)
        


if __name__ == '__main__':
    application.debug = True
    application.run()

I used CORS because I need it to serve as backend for my frontend which is already deployed on another domain. I know that as it’s configured, it’s unsafe for deployment, but just to make clear that it hasn’t been a problem

**Implementation details: **

  • I added a .ebignore file which contains the string virt (the python’s virtual environment)
  • I ran pip freeze > requirements.txt so EB knows the modules the app needs to run
  • Having everything ready, I cd to the directory and ran eb init -p python-3.11 myapp-deploy --region us-east-2
  • Ran eb init
  • Set up my SSH keypair
  • Ran eb create app-instance
  • Waited 4 minutes and it said the app was succesfully deployed. Ran eb open, and got to that domain on my browser which explicitly said 502 Bad Gateway - nginx
  • Checking the health status it said a 100% petitions were rejected and went from “Severe” to “Degrade”.
  • I terminated the instance.

Within my limited knowledge of servers, deploying, AWS, I’ve tried to find an error with the way the API interacts with server but I haven’t get so far yet, so I expect someone who knows about this and is willing to help me, may tell me what to fix and how to do it (what tools needed, commands, etc.) in order to solve this problem. Thanks a lot beforehand.

New contributor

paulomeister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật