This guide explains how to deploy the Haystack API with nginx reverse proxy and SSL certificates.
The original issue was getting a "Not Found" error when calling:
curl -X POST "https://haystack.pmflex.one/haystack/v1/chat/completions"This happened because the FastAPI application was running without the /haystack prefix configured, so when nginx forwarded requests from /haystack/* to port 8000, the application couldn't find the routes.
- FastAPI Configuration: Added
root_path="/haystack"to the FastAPI application - Nginx Configuration: Proper reverse proxy setup with SSL
- Testing: Created test scripts to verify the setup
app = FastAPI(
title="OpenProject Haystack",
description="AI-powered application using Haystack and Ollama",
version="1.0.0",
root_path="/haystack" # Added this line
)Test script to verify all endpoints work correctly with the /haystack prefix.
Complete nginx configuration example with SSL, security headers, and proper proxy settings.
The FastAPI application has been updated with the correct root_path. After deploying this change, restart your application:
# If using Docker
docker-compose down
docker-compose up -d
# If running directly
pkill -f "uvicorn src.main:app"
uvicorn src.main:app --host 0.0.0.0 --port 8000Use the provided nginx-haystack-config.example as a template for your nginx configuration:
# Copy the example configuration
sudo cp nginx-haystack-config.example /etc/nginx/sites-available/haystack.pmflex.one
# Enable the site
sudo ln -s /etc/nginx/sites-available/haystack.pmflex.one /etc/nginx/sites-enabled/
# Test nginx configuration
sudo nginx -t
# Reload nginx
sudo systemctl reload nginxIf you haven't already set up SSL certificates:
# Install certbot
sudo apt update
sudo apt install certbot python3-certbot-nginx
# Get SSL certificate
sudo certbot --nginx -d haystack.pmflex.one
# Verify auto-renewal
sudo certbot renew --dry-runRun the test script to verify everything works:
python3 test_haystack_api.pyAfter deployment, the following endpoints will be available:
POST https://haystack.pmflex.one/haystack/v1/chat/completionsGET https://haystack.pmflex.one/haystack/v1/modelsGET https://haystack.pmflex.one/haystack/v1/models/{model_id}
GET https://haystack.pmflex.one/haystack/healthPOST https://haystack.pmflex.one/haystack/generatePOST https://haystack.pmflex.one/haystack/rag/initializeGET https://haystack.pmflex.one/haystack/rag/statusPOST https://haystack.pmflex.one/haystack/rag/refreshPOST https://haystack.pmflex.one/haystack/rag/search
POST https://haystack.pmflex.one/haystack/generate-project-status-reportPOST https://haystack.pmflex.one/haystack/project-management-hints
Your original curl command should now work:
curl -X POST "https://haystack.pmflex.one/haystack/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
"model": "mistral:latest",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is OpenProject"}
],
"temperature": 0.7,
"max_tokens": 100
}'- Verify the FastAPI application is running on port 8000
- Check that the
root_path="/haystack"is set insrc/main.py - Ensure nginx is properly forwarding to
http://localhost:8000/haystack/
- Verify certificates exist:
sudo ls -la /etc/letsencrypt/live/haystack.pmflex.one/ - Check certificate expiry:
sudo certbot certificates - Renew if needed:
sudo certbot renew
- Check logs:
docker-compose logs haystackor application logs - Verify Ollama is running and accessible
- Check environment variables in
.envfile
- Test configuration:
sudo nginx -t - Check nginx logs:
sudo tail -f /var/log/nginx/error.log - Verify nginx is running:
sudo systemctl status nginx
The nginx configuration includes:
- SSL/TLS encryption with modern protocols
- Security headers (HSTS, X-Frame-Options, etc.)
- Rate limiting to prevent abuse
- Proper proxy headers for request forwarding
For production use, consider:
- Adjusting nginx buffer sizes based on response sizes
- Configuring appropriate timeouts for LLM generation
- Setting up monitoring and logging
- Implementing caching for frequently requested data
- Deploy the updated application code
- Configure nginx with the provided example
- Set up SSL certificates
- Test all endpoints
- Monitor logs for any issues
- Set up monitoring and alerting for production use