Reverse Proxy Configuration Guide
This comprehensive guide will help you configure various reverse proxy servers to work seamlessly with Ltranslate for automatic website translation.
Overview
Reverse proxy configuration allows you to integrate Ltranslate translation services with any web server or proxy solution. This guide covers the most popular proxy servers and CDN services.
Nginx Reverse Proxy
Basic Configuration
server {
listen 80;
server_name your-domain.com;
# Original content
location / {
proxy_pass http://your-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;
}
# Translation service
location ~ ^/(zh-cn|ja|ko|es|fr|de)/ {
proxy_pass https://engine.ltranslate.cn;
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 api-key "YOUR_API_KEY";
proxy_set_header source-lang "en";
proxy_set_header target-lang $1;
}
}
Apache Reverse Proxy
Enable Required Modules
# Enable proxy modules
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo systemctl restart apache2
Virtual Host Configuration
<VirtualHost *:80>
ServerName your-domain.com
# Original content
ProxyPreserveHost On
ProxyPass / http://your-backend/
ProxyPassReverse / http://your-backend/
# Translation service
ProxyPassMatch ^/(zh-cn|ja|ko|es|fr|de)/ https://engine.ltranslate.cn/
ProxyPassReverseMatch ^/(zh-cn|ja|ko|es|fr|de)/ https://engine.ltranslate.cn/
# Set custom headers
RequestHeader set api-key "YOUR_API_KEY"
RequestHeader set source-lang "en"
</VirtualHost>
Cloudflare Configuration
Origin Rules
Use Cloudflare Origin Rules to route translation requests to Ltranslate:
# Example Cloudflare Worker for translation
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
// Check if this is a translation request
if (url.pathname.match(/^\/(zh-cn|ja|ko|es|fr|de)\//)) {
const targetLang = url.pathname.split('/')[1]
const translationUrl = `https://engine.ltranslate.cn${url.pathname}`
const translationRequest = new Request(translationUrl, {
method: request.method,
headers: {
...request.headers,
'api-key': 'YOUR_API_KEY',
'source-lang': 'en',
'target-lang': targetLang,
'host': url.hostname
}
})
return fetch(translationRequest)
}
// Forward original request
return fetch(request)
}
HAProxy Configuration
Frontend and Backend Configuration
frontend https_frontend
bind *:443 ssl crt /path/to/cert.pem
bind *:80
# ACL for translation requests
acl is_translation_req path_beg /zh-cn/ /ja/ /ko/ /es/ /fr/ /de/
# Route to translation service
use_backend ltranslate_backend if is_translation_req
default_backend your_backend
backend ltranslate_backend
server ltranslate engine.ltranslate.cn:443 ssl
http-request set-header api-key "YOUR_API_KEY"
http-request set-header source-lang "en"
http-request set-header target-lang %{+path;regsub(^\/([^\/]+).*,$1)}
backend your_backend
server your_app localhost:8080
Traefik Configuration
Docker Compose Setup
version: '3.8'
services:
traefik:
image: traefik:v2.10
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
your-app:
image: your-app:latest
labels:
- "traefik.enable=true"
- "traefik.http.routers.your-app.rule=Host(`your-domain.com`)"
ltranslate:
image: traefik/whoami
labels:
- "traefik.enable=true"
- "traefik.http.routers.ltranslate.rule=Host(`your-domain.com`) && PathRegexp(`/zh-cn|ja|ko|es|fr|de}/`)"
- "traefik.http.middlewares.ltranslate-headers.headers.customrequestheaders.api-key=YOUR_API_KEY"
- "traefik.http.middlewares.ltranslate-headers.headers.customrequestheaders.source-lang=en"
Caddy Server Configuration
Caddyfile Setup
your-domain.com {
# Handle translation requests
handle_path /zh-cn* {
reverse_proxy https://engine.ltranslate.cn {
header_up api-key "YOUR_API_KEY"
header_up source-lang "en"
header_up target-lang "zh-cn"
}
}
handle_path /ja* {
reverse_proxy https://engine.ltranslate.cn {
header_up api-key "YOUR_API_KEY"
header_up source-lang "en"
header_up target-lang "ja"
}
}
# Original content
reverse_proxy http://your-backend:8080
}
IIS Configuration
URL Rewrite Rules
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Translation" stopProcessing="true">
<match url="^(zh-cn|ja|ko|es|fr|de)/(.*)" />
<action type="Rewrite" url="https://engine.ltranslate.cn/{R:0}" />
<serverVariables>
<set name="HTTP_API_KEY" value="YOUR_API_KEY" />
<set name="HTTP_SOURCE_LANG" value="en" />
<set name="HTTP_TARGET_LANG" value="{R:1}" />
</serverVariables>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Security Considerations
API Key Protection
- Never expose API keys in client-side code
- Use environment variables for sensitive configuration
- Implement IP whitelisting when possible
- Regularly rotate API keys
SSL/TLS Configuration
- Always use HTTPS for translation requests
- Implement proper certificate validation
- Consider HSTS headers for enhanced security
Rate Limiting
- Implement rate limiting to prevent abuse
- Monitor API usage patterns
- Set up alerts for unusual activity
Troubleshooting
Common Issues
502 Bad Gateway
Cause: Backend service unavailable or incorrect proxy configuration
Solution: Check backend service status and verify proxy settings
CORS Errors
Cause: Missing or incorrect CORS headers
Solution: Add appropriate CORS headers in your proxy configuration
SSL Certificate Issues
Cause: Invalid or expired SSL certificates
Solution: Update certificates and verify chain configuration
Best Practices
- Implement caching to reduce API calls and improve performance
- Use connection pooling for better resource utilization
- Monitor proxy performance and set up health checks
- Implement proper logging and monitoring
- Test configuration changes in staging environment first