Skip to content

Commit

Permalink
Nginx w/ multiple instances, debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmarieta committed Jan 8, 2024
1 parent f3a3ed8 commit f6a7194
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
23 changes: 18 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ services:
timeout: 5s
retries: 3

flask:
flask_1:
build: ./flask
container_name: flask
container_name: flask_1
env_file:
- .env
depends_on:
# waiting for the upstream containers to be ready
redis:
condition: service_healthy
db:
condition: service_healthy
volumes:
- ./flask:/app

flask_2:
build: ./flask
container_name: flask_2
env_file:
- .env
ports:
- "5000:5000"
depends_on:
# waiting for the upstream containers to be ready
redis:
Expand All @@ -52,7 +64,8 @@ services:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/ssl-certs:/etc/nginx/ssl-certs
depends_on:
- flask
- flask_1
- flask_2

# to persist container volumes
volumes:
Expand Down
4 changes: 2 additions & 2 deletions flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ EXPOSE 5000
RUN adduser --disabled-password --gecos '' flaskuser
USER flaskuser

# CMD python application.py
CMD python application.py
# CMD python application.py run --debug --host 0.0.0.0

# without Flask-SocketIO
# CMD gunicorn --access-logfile '-' --workers 1 --threads 4 --bind flask:5000 application:application
# with Flask-SocketIO
CMD gunicorn --access-logfile '-' --worker-class eventlet --workers 1 --threads 4 --bind flask:5000 application:application
# CMD gunicorn --access-logfile '-' --worker-class eventlet --workers 1 --threads 4 --bind flask:5000 application:application
14 changes: 11 additions & 3 deletions flask/application.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Flask
from app import create_app, db
from flask import Flask, jsonify
from app import create_app, db, socketio
from flask.cli import FlaskGroup

application = create_app()
Expand All @@ -11,5 +11,13 @@ def create_all():
with application.app_context():
db.create_all()

import os
HOSTNAME = os.uname().nodename

@application.route('/hostname')
def get_hostname():
return jsonify({'hostname': HOSTNAME})

if __name__ == "__main__":
cli()
# cli()
socketio.run(application, debug=True, host='0.0.0.0')
11 changes: 9 additions & 2 deletions nginx/conf.d/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ server {
return 301 https://$host$request_uri;
}

upstream flask_nodes {
ip_hash;

server flask_1:5000;
server flask_2:5000;
}

server {
listen 443 ssl;
server_name localhost;
Expand All @@ -14,15 +21,15 @@ server {
ssl_certificate_key /etc/nginx/ssl-certs/localhost.key;

location / {
proxy_pass http://flask:5000/;
proxy_pass http://flask_nodes/;
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-Prefix /;
}

location /socket.io {
proxy_pass http://flask:5000/socket.io;
proxy_pass http://flask_nodes/socket.io;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
Expand Down

0 comments on commit f6a7194

Please sign in to comment.