Serve your ML model with Gunicorn & Nginx. Step-by-step guide for setup, deployment, and performance optimization.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
// Import libraries
from flask import Flask, request, jsonify
import joblib // A library for model persistence
// Initialize the Flask application
app = Flask(**name**)
// Load your pre-trained ML model
model = joblib.load("model.joblib") // Replace with your actual model path
// Define a route for predictions
@app.route("/predict", methods=["POST"])
def predict():
input\_data = request.json // Extract JSON data from the request
prediction = model.predict([input\_data["features"]]) // Run the model prediction
return jsonify({"prediction": prediction.tolist()}) // Return result as JSON
// Entry point for Gunicorn to serve the app via WSGI
if **name** == "**main**":
app.run(debug=True)
app.py) is the WSGI entry point for Gunicorn.
app inside app.py.
// Example command to start Gunicorn with 4 worker processes
gunicorn --workers 4 --bind 127.0.0.1:8000 app:app
// Explanation:
// --workers 4 // Specifies the number of worker processes to handle incoming requests.
// --bind 127.0.0.1:8000 // Binds the server to localhost on port 8000.
// app:app // Indicates that the WSGI application callable is "app" in the "app.py" file.
ml_model_nginx.conf) with the following directives:
// Example Nginx configuration:
server {
listen 80; // Listen on port 80
server_name your_ml_model_domain.com; // Replace with your domain
location / {
proxy\_pass http://127.0.0.1:8000; // Forward requests to Gunicorn on localhost:8000
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;
}
// Optionally, serve static files here if your application has any
}
/etc/nginx/sites-available/ on many Linux distributions) and create a symbolic link to sites-enabled.
proxy_read_timeout and client_body_timeout to handle long-running ML predictions if necessary.
http://127.0.0.1:8000/predict and ensure the JSON response returns correctly.http://your_ml_model\_domain.com/predict. Use tools like curl or Postman for testing API endpoints./var/log/nginx/access.log and /var/log/nginx/error.log. Analyze these logs for proxy errors and connection issues.
From startups to enterprises and everything in between, see for yourself our incredible impact.
Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We’ll discuss your project and provide a custom quote at no cost.