Optimizing Nginx Reverse Proxy Performance with Apache Backend: Troubleshooting Slow Loading Issues


2 views

When your Nginx reverse proxy configuration isn't performing optimally with an Apache backend, several common patterns emerge. The symptoms you're describing - slow page loads and unexpected behavior - typically stem from three main areas:

1. Proxy configuration inefficiencies
2. Cache misconfiguration
3. Resource handling bottlenecks

Your current setup has several potential performance killers. Let's analyze the critical sections:

proxy_buffering Off;  # This forces synchronous operations
proxy_connect_timeout 300s;  # Excessive timeout values
proxy_read_timeout 300s;
proxy_send_timeout 300s;

These settings create unnecessary delays. Here's an optimized version:

proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 16k;
proxy_connect_timeout 60s;
proxy_read_timeout 90s;
proxy_send_timeout 90s;

The current cache configuration lacks proper tuning. Notice these problematic areas:

proxy_cache_key "$host$request_uri";  # Could be too broad
proxy_cache_min_uses 3;  # Too high for dynamic content
proxy_cache_valid 200 6h;  # Too long for some content types

Here's a smarter cache configuration:

proxy_cache_key "$scheme$host$request_uri$is_args$args";
proxy_cache_min_uses 1;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m 
    use_temp_path=off max_size=1g;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;

The static files handling in your location block can be optimized:

location ~* \.(3gp|gif|jpg|jpeg|png|ico|css|js|woff|ttf|svg|eot)$ {
    expires max;
    add_header Cache-Control public;
    add_header X-Cache-Status $upstream_cache_status;
    proxy_cache_bypass $http_cache_control;
    proxy_cache_valid 200 302 12h;
    access_log off;
    try_files $uri @apachebackend;
}

Add these directives to your http block for better connection handling:

upstream apache_backend {
    server xx.xx.xx.xx:8080;
    keepalive 32;
}

server {
    ...
    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://apache_backend;
    }
}
  1. Enable gzip compression for text-based content
  2. Implement HTTP/2 support
  3. Verify kernel TCP stack settings
  4. Monitor cache hit ratios
  5. Adjust worker processes based on CPU cores

When your Nginx reverse proxy setup with Apache backend starts exhibiting sluggish response times, several configuration elements could be at play. The symptoms you're describing - delayed page loads and suboptimal performance - typically stem from either improper caching mechanisms, buffer misconfigurations, or inefficient proxy handling.

Your current setup shows some good practices but also contains potential bottlenecks:

# Inefficiencies in the current proxy.conf
proxy_buffering Off;  # This might actually hurt performance
proxy_buffers 100 128k;  # Excessive number of buffers
proxy_connect_timeout 300s;  # Unnecessarily long timeout

Let's rebuild your proxy configuration with performance in mind:

# Optimized proxy.conf
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
proxy_busy_buffers_size 16k;
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=nginx-cache:100m inactive=24h max_size=1g;
proxy_temp_path /var/cache/nginx/tmp;

proxy_connect_timeout 30s;
proxy_read_timeout 120s;
proxy_send_timeout 120s;

proxy_cache_lock on;
proxy_cache_lock_age 10s;
proxy_cache_lock_timeout 10s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_cache_revalidate on;

Your server block needs these crucial adjustments:

server {
    listen xx.xx.xx.xx:80 reuseport;  # Add reuseport for better connection handling
    server_name domain.com www.domain.com;

    # Static files handling - optimized
    location ~* \.(3gp|gif|jpg|jpeg|png|ico|css|js|woff2?|ttf|svg|eot)$ {
        root /home/username/public_html;
        expires 30d;
        access_log off;
        add_header Cache-Control "public";
        try_files $uri @apachebackend;
    }

    # Dynamic content handling
    location / {
        proxy_pass http://xx.xx.xx.xx:8080;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        include /usr/local/nginx/conf/optimized_proxy.conf;
    }

    location @apachebackend {
        proxy_pass http://xx.xx.xx.xx:8080;
        proxy_cache nginx-cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        include /usr/local/nginx/conf/optimized_proxy.conf;
    }
}

Implement these additional performance enhancements:

# In nginx.conf main context
worker_processes auto;
worker_rlimit_nofile 100000;

events {
    worker_connections 4000;
    use epoll;
    multi_accept on;
}

http {
    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    # TCP optimizations
    tcp_nopush on;
    tcp_nodelay on;
    sendfile on;
}

After implementing these changes, verify performance with:

# Check active connections
ss -lntp | grep nginx

# Monitor cache status
tail -f /var/log/nginx/cache.log

# Benchmark tool (install if needed)
ab -n 5000 -c 100 http://domain.com/test.html