[Python] How to add NGINX to a Python app on Heroku?

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get nginx working on my flask app with hosting on heroku. My app is using flask-socketio. I'm getting this error app[web.1]: nginx: [emerg] bind() to 0.0.0.0:27318 failed (98: Address already in use). I tried using a single '&' in the Procfile and it loops this error in the console app[web.1]: buildpack=nginx at=app-initialization

    Here are my files.

    nginx.conf

    worker_processes 1;

    events {
    worker_connections 1024;
    }

    http {
    include mime.types;
    sendfile on;

    server_tokens off; # Hide Nginx version
    more_set_headers "Server: Secure"; # Override server header

    upstream flask_backend {
    server 127.0.0.1:5000; # Ensure Gunicorn is running on this port
    }

    server {
    listen 8080;

    location / {
    proxy_pass http://flask_backend;
    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_hide_header X-Powered-By;
    proxy_hide_header Server;
    more_clear_headers 'X-Powered-By' 'Server';

    proxy_buffering off;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    }
    }


    }

    Procfile

    web: bin/start-nginx && gunicorn -c config/gunicorn.conf.py -k
    eventlet -w 1 app:app --bind 0.0.0.0:$PORT


    app.py

    if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000)) # Use Heroku-assigned port
    socketio.run(app, host="0.0.0.0", port=port)


    gunicorn.conf.py

    import os
    import multiprocessing

    bind = "unix:/tmp/nginx.socket"
    workers = multiprocessing.cpu_count() * 2 + 1

    def on_starting(server):
    open("/tmp/app-initialized", "w").close()


    Is there a way around this?

    Continue reading...

Compartilhe esta Página