Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nginx refused to connect and showing bad gateway error. #225

Open
razaaliabid87 opened this issue Jul 19, 2021 · 3 comments
Open

Nginx refused to connect and showing bad gateway error. #225

razaaliabid87 opened this issue Jul 19, 2021 · 3 comments

Comments

@razaaliabid87
Copy link

I am working on Nginx, Flask_SocketIO, and uWSGI+Gevent. Whenever I run my code in the docker container I always ended up with a 502 Bad Gateway error. But the flask socketio application runs perfectly in development when I don't use Nginx and uWSGI.
Bad Gateway
nginx.conf

server {
    listen 80;
    location /ztest {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_pass http://0.0.0.0:8085;
    }
    location /socket.io {
    
        proxy_pass http://0.0.0.0:8085/socket.io;
        proxy_redirect off;
        proxy_buffering off;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    location /static/ {
        location ~* \.(js|css)$ {
            expires 30d;
        }
        expires 1h;
        alias /home/%(username)s/my_app_name/static/;
        access_log off;
        log_not_found off;
    }
}

uWSGI configuration resides in the .ini file.
app.ini

[uwsgi]
wsgi-file = application.py
callable = app
mount = /=application.py
gevent-early-monkey-patch = 1
gevent = 1000
processes = 4
threads = 2
master = true
chmod-socket = 660
vacuum = true
manage-script-name = true
die-on-term = true
http = :8085
http-websockets = true
single-interpreter = true

docker-compose.yml

version: "3.9"
services:
  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    ports:
      - "80:80"
 
  ztest:
    build: ./ztest
    container_name: ztest
    restart: always
    environment:
      - APP_NAME=Myztest
    expose:
      - 8085

application.py.

import gevent
gevent.monkey.patch_all()

from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

socketio = SocketIO(app, async_mode=None, logger=True, engineio_logger=True)
thread = Thread()
thread_stop_event = Event()

def randomNumberGenerator():
    print("Making random numbers")
    while not thread_stop_event.isSet():
        number = round(random()*10, 3)
        print(number)
        socketio.emit('newnumber', {'number': number}, namespace='/test')
        socketio.sleep(5)

@app.route('/ztest')
def index():
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    global thread
    print('Client connected')
    thread = socketio.start_background_task(randomNumberGenerator)

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

if __name__ == '__main__':
    socketio.run(app, host="0.0.0.0", port=8085)

requirements.txt

bidict==0.21.2
cffi==1.14.6
click==8.0.1
colorama==0.4.4
Flask==1.0.2
Flask-SocketIO==4.3.1
gevent==21.1.2
gevent-websocket==0.10.1
greenlet==1.1.0
gunicorn==20.1.0
itsdangerous==2.0.1
Jinja2==3.0.1
MarkupSafe==2.0.1
pycparser==2.20
python-engineio==3.13.2
python-socketio==4.6.0
six==1.16.0
Werkzeug==2.0.1
zope.event==4.5.0
zope.interface==5.4.0
uWSGI==2.0.19.1

When I build and run nginx and ztest docker containers, I am unable to reach my desired endpoint. Nginx gives me a Bad Gateway error. Here are the logs.

nginx       | 2021/07/19 21:09:10 [error] 23#23: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /ztest HTTP/1.1", upstream: "http://0.0.0.0:8085/ztest", host: "192.168.10.7"

Please help me to get out of this problem thank you in anticipation.

@mullenkamp
Copy link

I seem to have this same problem. I've built a bunch of web apps on this stack in the past and have never gotten this error before...but it's random...most of the time it doesn't work...but occationally it does...I'll do some digging.

Nevertheless, thank you very much for this stack!

@mullenkamp
Copy link

I suspect this is caused by the issue raised here:
#192

@quantitative-technologies
Copy link

quantitative-technologies commented Oct 6, 2022

I get this iff I set app.config['SECRET_KEY'] = ... in my flask app.

Edit. Never mind, I checked my flask logs and the app was crashing due to a bug in my python script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants