ragflow.conf
Overview
The ragflow.conf file is an Nginx server configuration file designed to serve the frontend assets of the Ragflow web application and to proxy API requests to the backend service. It primarily configures HTTP settings such as gzip compression, static file caching, and routing rules for frontend and API endpoints.
This configuration ensures efficient delivery of static files (JavaScript, CSS, media), enables compression to reduce bandwidth, and directs API requests to the appropriate backend server.
Detailed Explanation
Server Block
server {
listen 80;
server_name _;
root /ragflow/web/dist;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
location ~ ^/(v1|api) {
proxy_pass http://ragflow:9380;
include proxy.conf;
}
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
location ~ ^/static/(css|js|media)/ {
expires 10y;
access_log off;
}
}
Configuration Directives and Their Purpose
1. listen 80;
Purpose: Listens for incoming HTTP requests on port 80.
Usage: Standard for serving web traffic over HTTP.
2. server_name _;
Purpose: Matches any server name not matched by other server blocks.
Usage: Acts as a catch-all server block.
3. root /ragflow/web/dist;
Purpose: Sets the root directory for serving static files.
Usage: This is where the frontend build artifacts (e.g., React or Vue compiled files) are located.
Gzip Compression Configuration
gzip on;— Enables gzip compression.gzip_min_length 1k;— Only compress responses larger than 1KB.gzip_comp_level 9;— Maximum compression level (best compression).gzip_types ...— Specifies MIME types to compress, including JS, CSS, XML, images, and PHP.gzip_vary on;— Enables theVary: Accept-Encodingheader for proxies.gzip_disable "MSIE [1-6]."— Disables gzip for old Internet Explorer versions (1 to 6).
Implementation detail: This configuration optimizes bandwidth usage for supported clients by compressing responses, improving page load times.
Location Blocks
1. API Proxy Location
location ~ ^/(v1|api) {
proxy_pass http://ragflow:9380;
include proxy.conf;
}
Purpose: Proxies requests starting with
/v1or/apito the backend service athttp://ragflow:9380.Parameters:
proxy_passdirects the request to backend.include proxy.conflikely adds additional proxy settings such as headers and timeouts (note: the contents ofproxy.confare external to this file).
Usage Example:
Request to
http://server/api/usersis forwarded tohttp://ragflow:9380/api/users.
Implementation detail: Using a regex location for versioned API paths allows flexible routing for backend endpoints.
2. Frontend Root Location
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
Purpose: Serves the frontend application.
Behavior:
If the requested URI corresponds to a file or directory, serve it.
Otherwise, serve
index.html.
Usage: Supports client-side routing frameworks (e.g., React Router) by falling back to
index.htmlfor unmatched routes.
3. Static Assets Cache Location
location ~ ^/static/(css|js|media)/ {
expires 10y;
access_log off;
}
Purpose: Serves static assets like CSS, JS, and media files with a long cache expiry.
Behavior:
Sets
Cache-Controlheader to expire assets after 10 years.Disables access logging to reduce log size for static files.
Usage: Improves performance by allowing browsers to cache static resources aggressively.
Interaction with Other Parts of the System
Backend Service: The proxy location forwards API requests to the backend running at
ragflow:9380. This suggests a microservices or containerized environment whereragflowis a service name or hostname.Frontend Application: The root
/location serves the frontend JavaScript application located in/ragflow/web/dist.Proxy Config: The
include proxy.conf;directive pulls in additional proxy settings, which may include headers, buffering, timeouts, or SSL configuration, allowing modular and maintainable configuration.
Usage Examples
1. Accessing the Frontend
Navigate to
http://<server>/.The server serves
/ragflow/web/dist/index.html.Static assets like JS and CSS are served with long cache expiry.
2. API Request Proxying
Make an API call to
http://<server>/api/data.Nginx proxies this request to
http://ragflow:9380/api/data.
Important Implementation Details
Regex Location Matching: The use of regular expressions in locations (
location ~ ^/(v1|api)) allows flexible matching for API routes.Try Files Directive:
try_files $uri $uri/ /index.html;is a common SPA (Single Page Application) pattern enabling client-side routing without server support.Long Cache Duration: Setting
expires 10y;for static assets assumes fingerprinted file names to avoid stale cache issues.Gzip Configuration: Compresses a broad range of content types, carefully disables gzip for old browsers known to have issues.
Mermaid Diagram
flowchart TD
A[Incoming HTTP Request] --> B{URI Match}
B -->|Starts with /v1 or /api| C[Proxy to Backend at ragflow:9380]
B -->|Starts with /static/css, /static/js, /static/media| D[Serve Static Asset with 10y Cache]
B -->|Other URIs| E[Try Files: Serve File or Directory]
E -->|Not Found| F[Serve /index.html (SPA Fallback)]
Summary
The ragflow.conf file is a focused Nginx configuration for serving a modern web application with:
Frontend static file hosting tailored for SPA frameworks.
API request proxying to backend services.
Performance optimizations via gzip compression and long-lived caching.
Compatibility considerations for legacy browsers.
It acts as a critical bridge between users and the Ragflow application, managing HTTP traffic efficiently and reliably.