Nginx & Supervisord Configurations
Relevant source files
The following files were used as context for generating this wiki page:
The routing and process management infrastructure of the bot-auto-ig project ensures seamless communication between the React-based frontend, the Express API server, and the Python-driven automation engine. Nginx acts as the primary reverse proxy and web server, while the backend server handles the lifecycle of automation processes.
This configuration facilitates the distribution of static assets, handles API request proxying, and maintains persistent WebSocket connections for real-time logging and status updates.
Nginx Architecture and Routing
Nginx serves as the entry point for all HTTP traffic. It is configured to serve static frontend files and proxy dynamic requests to the appropriate backend services.
Core Routing Rules
The configuration defines specific locations for handling different types of traffic:
- Static Assets: Serves the frontend application from
/usr/share/nginx/html. - Data Uploader API: Proxies requests starting with
/api/datauploader/to a dedicated service. - Main API: Routes general
/api/requests to the core backend server. - WebSockets: Manages protocol upgrades for persistent connections at
/ws.
Reverse Proxy Configuration
Nginx ensures that client information is preserved when forwarding requests by setting standard headers such as X-Real-IP and X-Forwarded-For.
| Location | Target Service | Purpose |
|---|---|---|
/ | Local Filesystem | Serves index.html and static JS/CSS |
/api/datauploader/ | http://datauploader:3002/ | Ingests CSV and workflow artifacts |
/api/ | http://server:3001 | Core Express API (Automation, Profiles, Logs) |
/ws | http://server:3001/ws | Real-time WebSocket event stream |
Sources: frontend/nginx.conf:1-35, AGENTS.md:25-32
The following diagram illustrates the flow of traffic through the Nginx reverse proxy:
This diagram shows how Nginx differentiates between static content and service-specific API requests. Sources: frontend/nginx.conf:10-34
Backend Process Management
While Nginx handles external traffic, the Express server manages the internal lifecycle of the application and its subprocesses. The project utilizes a structured approach to starting, monitoring, and shutting down services.
Process Lifecycle and Orchestration
The server (typically managed by a process manager like Supervisord in production environments) initializes several critical components upon startup:
- WebSocket Initialization: Establishes the server for real-time event broadcasting.
- Shutdown Handlers: Registers listeners for
SIGTERMandSIGINTto ensure graceful termination of HTTP and WebSocket servers. - Orphan Cleanup: Automatically identifies and kills Python subprocesses left behind by previous sessions.
- State Recovery: Detects interrupted automation runs and reconciles profile statuses in the database.
Sources: server/index.ts:121-140, server/index.ts:145-163
Python Automation Subprocesses
The backend acts as a controller for the Python-based automation runtime. When a workflow starts, the server spawns a Python subprocess and manages its communication through standard I/O streams.
This sequence highlights the interaction between the server and the spawned Python automation script. Sources: server/automation/routes.ts:114-135, server/index.ts:14-16
WebSocket Connectivity
WebSockets are critical for providing real-time feedback from the automation engine to the user interface.
Connection and Proxying
Nginx is configured to support the Upgrade and Connection headers required for the WebSocket protocol. On the frontend, the useWebSocket hook manages the connection lifecycle, including authentication via Clerk tokens and automatic exponential backoff for reconnections.
| Configuration | Value | Source |
|---|---|---|
| Nginx WS Path | /ws | frontend/nginx.conf:29 |
| Backend Port | 3001 | server/index.ts:119 |
| Frontend Proxy | ws://localhost:3001 | frontend/vite.config.ts:111 |
| Reconnect Delay | Base 1s, Max 30s | frontend/src/hooks/useWebSocket.ts:80-84 |
Event Broadcasting
The server captures output from Python subprocesses (stdout/stderr), parses them into structured log objects, and broadcasts them to all connected clients.
Conclusion
The combination of Nginx for external request routing and the Express server for internal process orchestration creates a robust environment for running social media automation. Nginx ensures efficient delivery of the frontend and secure proxying of API and WebSocket traffic, while the backend server maintains the integrity of the automation state and provides real-time monitoring capabilities.