Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see:

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

History
730 lines (574 loc) · 17.4 KB

File metadata and controls

730 lines (574 loc) · 17.4 KB

Nginx Integration with Python-Proxy

This guide shows how to configure nginx to proxy traffic through python-proxy, leveraging its powerful hook system for request/response modification, URL rewriting, content injection, and more.

Overview

Nginx acts as the frontend server, forwarding requests to python-proxy with the X-Proxy-Server header. Python-proxy then applies configured hooks before proxying to the actual backend.

Architecture:

Client → Nginx → Python-Proxy (with hooks) → Backend Server
↓
Hook Processing

Prerequisites

  1. Python-proxy running:

    # Start python-proxy on port 8080
    python-proxy --port 8080 --config config.yaml
  2. Nginx installed:

    # Ubuntu/Debian
    sudo apt-get install nginx
    # CentOS/RHEL/Fedora
    sudo dnf install nginx

Basic Configuration

Note: In all examples below, python-proxy-ip:8080 should be replaced with:

  • localhost:8080 if python-proxy runs on the same server as nginx
  • 192.168.1.100:8080 (or actual IP) if python-proxy runs on a different server
  • python-proxy.local:8080 (or hostname) if using DNS/hosts file

Example 1: Proxy Entire Site Through Python-Proxy

Forward all traffic to python-proxy, which then proxies to the backend.

nginx.conf:

server{listen80;server_name example.com;location / { # Proxy to python-proxyproxy_passhttp://python-proxy-ip:8080; # Set backend server using X-Proxy-Server headerproxy_set_header X-Proxy-Server backend.example.com; # Preserve original hostproxy_set_header Host $host; # Forward client infoproxy_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;}}

What this does:

  • All requests to example.com go through python-proxy
  • Python-proxy applies configured hooks (URL rewriting, content modification, etc.)
  • Python-proxy forwards to backend.example.com

Example 2: Proxy Specific Paths Only

Only certain URLs go through python-proxy, others go directly to backend.

nginx.conf:

server{listen80;server_name example.com; # Only proxy /api/* through python-proxy for modificationlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;proxy_set_header Host $host;} # Static content goes directly to backendlocation /static/ {proxy_passhttp://backend.example.com;proxy_set_header Host $host;} # Everything else also goes through python-proxylocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Advanced Configurations

Example 3: Route Different Paths to Different Backends

Route different URL patterns to different backend servers through python-proxy.

nginx.conf:

server{listen80;server_name example.com; # API requests go to API backendlocation /api/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api.backend.local:8000;proxy_set_header Host $host;} # Blog requests go to WordPress backendlocation /blog/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Admin panel goes to admin backend with HTTPSlocation /admin/ {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server admin.backend.local:443;proxy_set_header Host $host;} # Everything elselocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server main.backend.local;proxy_set_header Host $host;}}

Example 4: Dynamic Backend Based on Request

Use nginx variables to dynamically set the backend server.

nginx.conf:

map$request_uri$backend_server{~^/api/v1/ "api-v1.backend.local:8001";~^/api/v2/ "api-v2.backend.local:8002";~^/legacy/ "legacy.backend.local:9000";
default "main.backend.local";}server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Example 5: Override Backend Host Header

Use X-Proxy-Host to override the Host header sent to the backend.

nginx.conf:

server{listen80;server_name public.example.com;location / {proxy_passhttp://python-proxy-ip:8080; # Backend server is an IPproxy_set_header X-Proxy-Server 192.168.1.100:8080; # But backend expects this hostnameproxy_set_header X-Proxy-Host internal.backend.local; # Keep original host for clientproxy_set_header Host $host;}}

Example 6: Load Balancing with Python-Proxy

Combine nginx load balancing with python-proxy hooks.

nginx.conf:

upstream python_proxy_cluster {server127.0.0.1:8080;server127.0.0.1:8081;server127.0.0.1:8082;}upstream backend_cluster {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server{listen80;server_name example.com;location / { # Load balance across python-proxy instancesproxy_passhttp://python_proxy_cluster; # All python-proxy instances will forward to backend cluster # (python-proxy can also do its own backend selection)proxy_set_header X-Proxy-Server backend1.example.com;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

Python-Proxy Configuration for Nginx

When using with nginx, configure python-proxy hooks for your use cases:

config.yaml:

host: "0.0.0.0"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Redirect old URLs before hitting backend
- hostname: "example.com"url_pattern: "/old-api/*"hook: "redirect_301"params:
location: "https://example.com/api/"post_hooks:
# Add .local to all links for local testing
- hostname: "example.com.local"url_pattern: "/*"hook: "link_rewrite"params:
from_domain: "example.com"to_domain: "example.com.local"# Inject analytics script
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'

Use Cases

Use Case 1: Local Development with .local Domains

Problem: Test production site locally without modifying /etc/hosts for every resource.

Solution:

# /etc/nginx/sites-available/local-devserver{listen80;server_name myapp.local;location / {proxy_passhttp://python-proxy-ip:8080; # Python-proxy will strip .local and forward to productionproxy_set_header Host myapp.local;}}

Python-proxy config:

  • Automatic .local domain stripping is built-in!
  • Links are rewritten using link_rewrite hook

Use Case 2: Content Injection for A/B Testing

Problem: Inject A/B testing scripts without modifying backend code.

nginx.conf:

server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header Host $host;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/*"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="https://cdn.example.com/ab-testing.js"></script>'

Use Case 3: API Version Migration

Problem: Migrate from /api/v1/ to /api/v2/ URLs in responses.

nginx.conf:

server{listen80;server_name api.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server api-backend.example.com;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "api.example.com"url_pattern: "/*"hook: "url_rewrite"params:
pattern: '"/api/v1/'replacement: '"/api/v2/'content_types: ["application/json", "text/html"]

Use Case 4: WordPress + Static Site Integration

Problem: Fetch blog content from WordPress, inject into static site.

nginx.conf:

server{listen80;server_name example.com; # Static sitelocation / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server static.backend.local;}}

Python-proxy config:

hook_mappings:
post_hooks:
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.example.com/wp-json/wp/v2/posts/123'source_xpath: '//article[@class="post"]'action: "replace_content"

Use Case 5: Multi-Tenant Backend Routing

Problem: Route different domains to different backend servers.

nginx.conf:

map$host$backend_server{
tenant1.example.com "tenant1.backend.local:8001";
tenant2.example.com "tenant2.backend.local:8002";
tenant3.example.com "tenant3.backend.local:8003";
default "default.backend.local:8000";}server{listen80;server_name *.example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server $backend_server;proxy_set_header Host $host;}}

Performance Considerations

Caching

Enable nginx caching to reduce load on python-proxy:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;server{listen80;server_name example.com;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Cache responses from python-proxyproxy_cache my_cache;proxy_cache_valid20010m;proxy_cache_valid4041m;proxy_cache_key"$scheme$request_method$host$request_uri";add_header X-Cache-Status $upstream_cache_status;}}

Connection Pooling

Keep connections alive between nginx and python-proxy:

upstream python_proxy {server127.0.0.1:8080;keepalive32;}server{listen80;server_name example.com;location / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server backend.example.com;}}

Debugging

Enable Debug Logging

Nginx:

error_log /var/log/nginx/error.log debug;server{access_log /var/log/nginx/access.log combined;location / {proxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com; # Log headersadd_header X-Debug-Backend backend.example.com;}}

Python-proxy:

log_level: "DEBUG"

Test Configuration

# Test nginx config
sudo nginx -t
# Reload nginx
sudo systemctl reload nginx
# Test python-proxy
curl -v http://localhost:8080/test \
-H "X-Proxy-Server: backend.example.com" \
-H "Host: example.com"# Test through nginx
curl -v http://example.com/test

Security Considerations

Restrict Python-Proxy Access

Python-proxy should only accept connections from nginx:

# Nginx config - no changes needed# Python-proxy - bind to localhost only
python-proxy --host 127.0.0.1 --port 8080

Sanitize Headers

Prevent clients from sending proxy control headers:

server{listen80;server_name example.com;location / { # Remove any client-set proxy headersproxy_set_header X-Proxy-Server "";proxy_set_header X-Proxy-Host ""; # Set your own backendproxy_set_header X-Proxy-Server backend.example.com;proxy_passhttp://python-proxy-ip:8080;}}

SSL/TLS Termination

Let nginx handle SSL, python-proxy handles HTTP:

server{listen443ssl http2;server_name example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;location / { # Nginx handles HTTPS, forwards HTTP to python-proxyproxy_passhttp://python-proxy-ip:8080;proxy_set_header X-Proxy-Server backend.example.com;proxy_set_header X-Forwarded-Proto $scheme;}}

Complete Example: Production Setup

Complete nginx configuration:

# /etc/nginx/sites-available/example.comupstream python_proxy {server127.0.0.1:8080;server127.0.0.1:8081;keepalive32;}# HTTP redirect to HTTPSserver{listen80;server_name example.com www.example.com;return301 https://example.com$request_uri;}# HTTPS serverserver{listen443ssl http2;server_name example.com www.example.com;ssl_certificate /etc/ssl/certs/example.com.crt;ssl_certificate_key /etc/ssl/private/example.com.key;ssl_protocols TLSv1.2 TLSv1.3; # Loggingaccess_log /var/log/nginx/example.com-access.log combined;error_log /var/log/nginx/example.com-error.log warn; # API routes through python-proxylocation /api/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server api.backend.local: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;} # Blog through python-proxy with WordPress integrationlocation /blog/ {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server wordpress.backend.local;proxy_set_header Host $host;} # Static assets bypass python-proxylocation /static/ {proxy_passhttp://static.backend.local;proxy_cache my_cache;proxy_cache_valid2001h;} # Everything else through python-proxylocation / {proxy_passhttp://python_proxy;proxy_http_version 1.1;proxy_set_header Connection "";proxy_set_header X-Proxy-Server main.backend.local;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;}}

Corresponding python-proxy config:

host: "127.0.0.1"port: 8080log_level: "INFO"hook_mappings:
pre_hooks:
# Maintenance mode for specific paths
- hostname: "example.com"url_pattern: "/maintenance"hook: "static_html"params:
html: | <!DOCTYPE html> <html><body><h1>Under Maintenance</h1></body></html>status: 503post_hooks:
# Inject analytics on all HTML pages
- hostname: "*"url_pattern: "/*.html"hook: "html_rewrite"params:
xpath: "//head"action: "insert_before"value: '<script src="/analytics.js"></script>'# Fetch WordPress content
- hostname: "example.com"url_pattern: "/blog/*"hook: "xpath_replace_from_url"params:
target_xpath: '//div[@id="blog-content"]'source_url: 'https://wordpress.backend.local/wp-json/wp/v2/posts/123'source_xpath: '//article'

Troubleshooting

Problem: Backend not receiving requests

Check:

  1. Python-proxy is running: curl http://localhost:8080
  2. Headers are set correctly in nginx: Check access logs
  3. Python-proxy can reach backend: Test manually

Problem: Infinite redirect loop

Cause: Python-proxy or backend returning redirects to nginx URL

Solution: Set X-Forwarded-Host properly:

proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Proto $scheme;

Problem: Headers not being set

Check nginx config:

# Verify configuration
sudo nginx -t
# Check what headers are actually sent
tcpdump -i lo -A 'port 8080'

Summary

Nginx + Python-Proxy provides a powerful combination:

  • Nginx: High-performance frontend, SSL termination, load balancing
  • Python-Proxy: Flexible request/response modification with hooks

This architecture enables:

  • ✅ Dynamic content injection
  • ✅ URL rewriting and normalization
  • ✅ Multi-source content aggregation
  • ✅ A/B testing and experimentation
  • ✅ Legacy system integration
  • ✅ Local development workflows

For more information, see: