# Fix Apache 502 Proxy Error
Apache returns "502 Proxy Error" when acting as a reverse proxy and can't connect to the backend server or the backend returns an invalid response.
Introduction
Apache returns "502 Proxy Error" when acting as a reverse proxy and can't connect to the backend server or the backend returns an invalid response.
502 Proxy Error
The proxy server received an invalid response from an upstream serverSymptoms
- HTTP 502 Proxy Error returned to clients
- Backend connection failures in Apache logs
- Timeout errors when connecting to upstream
- Invalid response from backend server
Common error messages:
[error] proxy: HTTP: attempt to connect to 127.0.0.1:8080 failed
[error] (111)Connection refused: proxy: HTTP: attempt to connect to 127.0.0.1:8080 failedCommon Causes
- Backend server not running
- Backend server timeout
- Backend server overloaded
- Network connectivity issues
- Misconfigured proxy settings
Step-by-Step Fix
Check Apache error logs:
```bash # View error logs tail -f /var/log/apache2/error.log tail -f /var/log/httpd/error_log
# Filter for proxy errors grep -i "proxy" /var/log/apache2/error.log | tail -50 ```
Check backend server:
```bash # Check if backend is running systemctl status backend-service
# Test backend directly curl -I http://localhost:8080
# Check backend port netstat -tlnp | grep 8080 ```
Check Apache modules:
```bash # List loaded modules apache2ctl -M | grep proxy
# Required modules: # proxy_module # proxy_http_module # proxy_balancer_module (if using balancer) ```
Common Causes and Solutions
Cause 1: Backend Server Not Running
[error] proxy: HTTP: attempt to connect to 127.0.0.1:8080 failedSolution:
```bash # Start backend server systemctl start backend-service
# Check backend status systemctl status backend-service
# Test backend directly curl http://localhost:8080/health ```
Cause 2: Backend Timeout
[error] proxy: read timeoutSolution:
```apache # Increase proxy timeout ProxyPass /app http://localhost:8080 timeout=300 ProxyPassReverse /app http://localhost:8080
# Or globally ProxyTimeout 300
# In VirtualHost <VirtualHost *:80> ProxyPreserveHost On ProxyTimeout 300
ProxyPass /app http://localhost:8080 connectiontimeout=300 timeout=300 ProxyPassReverse /app http://localhost:8080 </VirtualHost> ```
Cause 3: Backend Overloaded
[error] proxy: HTTP: disabled connection for 10 secondsSolution:
```apache # Configure retry and recovery ProxyPass /app http://localhost:8080 retry=5 timeout=60
# Use load balancer with multiple backends <Proxy balancer://mycluster> BalancerMember http://backend1:8080 retry=5 timeout=60 BalancerMember http://backend2:8080 retry=5 timeout=60 ProxySet lbmethod=byrequests </Proxy>
ProxyPass /app balancer://mycluster ProxyPassReverse /app balancer://mycluster ```
Cause 4: Connection Refused
[error] (111)Connection refused: proxy: HTTP: attempt to connect failedSolution:
```bash # Check backend is listening netstat -tlnp | grep 8080
# Check firewall iptables -L -n | grep 8080
# Check SELinux (RHEL/CentOS) getsebool -a | grep httpd_can_network_connect setsebool -P httpd_can_network_connect 1 ```
Cause 5: DNS Resolution Issues
[error] proxy: DNS lookup failure for: backend.example.comSolution:
```bash # Check DNS resolution nslookup backend.example.com dig backend.example.com
# Use IP address instead ProxyPass /app http://10.0.0.10:8080
# Or add to /etc/hosts echo "10.0.0.10 backend.example.com" >> /etc/hosts ```
Cause 6: SSL/TLS Issues
[error] proxy: SSL handshake failedSolution:
```apache # For HTTPS backend SSLProxyEngine On ProxyPass /app https://backend.example.com:8443 ProxyPassReverse /app https://backend.example.com:8443
# Disable SSL verification (not for production) SSLProxyVerify none SSLProxyCheckPeerCN off SSLProxyCheckPeerName off ```
Cause 7: Buffer Size Issues
[error] proxy: error reading from the serverSolution:
```apache # Increase buffer sizes ProxyIOBufferSize 65536
# In VirtualHost <VirtualHost *:80> ProxyIOBufferSize 65536 LimitRequestFieldSize 32768 LimitRequestBody 0
ProxyPass /app http://localhost:8080 ProxyPassReverse /app http://localhost:8080 </VirtualHost> ```
Complete Proxy Configuration
Basic Reverse Proxy
```apache <VirtualHost *:80> ServerName example.com
ProxyPreserveHost On ProxyTimeout 300
# Health check endpoint ProxyPass /health http://localhost:8080/health retry=0
# Main application ProxyPass / http://localhost:8080/ connectiontimeout=300 timeout=300 ProxyPassReverse / http://localhost:8080/
# Error handling ProxyErrorOverride On ErrorDocument 502 /502.html </VirtualHost> ```
Load Balanced Configuration
```apache <Proxy balancer://appcluster> BalancerMember http://app1:8080 retry=5 timeout=60 loadfactor=1 BalancerMember http://app2:8080 retry=5 timeout=60 loadfactor=1 BalancerMember http://app3:8080 retry=5 timeout=60 loadfactor=1 ProxySet lbmethod=byrequests failonstatus=502,503 ProxySet retry=5 </Proxy>
<VirtualHost *:80> ServerName example.com
ProxyPreserveHost On ProxyPass / balancer://appcluster/ stickysession=JSESSIONID ProxyPassReverse / balancer://appcluster/
# Balancer manager (for monitoring) <Location /balancer-manager> SetHandler balancer-manager Require ip 10.0.0.0/8 </Location> </VirtualHost> ```
WebSocket Proxy
```apache <VirtualHost *:80> ServerName example.com
# WebSocket upgrade RewriteEngine On RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} upgrade [NC] RewriteRule /ws/(.*) ws://localhost:8080/ws/$1 [P,L]
# Regular HTTP proxy ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ </VirtualHost> ```
Monitoring and Logging
```apache # Enable proxy logging LogLevel proxy:debug ErrorLog /var/log/apache2/proxy_error.log
# Custom log format with backend info LogFormat "%h %l %u %t \"%r\" %>s %b \"%{X-Forwarded-For}i\" \"%{BALANCER_WORKER_ROUTE}e\"" proxy_log CustomLog /var/log/apache2/proxy_access.log proxy_log ```
Verification
```bash # Test proxy configuration apache2ctl configtest
# Check backend connectivity curl -I http://localhost:8080
# Test through proxy curl -I http://example.com
# Check Apache status systemctl status apache2
# View proxy logs tail -f /var/log/apache2/error.log | grep proxy ```
Prevention
- 1.[ ] Backend server is running
- 2.[ ] Backend port is accessible
- 3.[ ] Proxy modules are loaded
- 4.[ ] Proxy configuration is correct
- 5.[ ] Timeout values are appropriate
- 6.[ ] Firewall allows connections
- 7.[ ] SELinux allows network connections
- 8.[ ] DNS resolution works
- 9.[ ] SSL/TLS configured correctly
- 10.[ ] Check Apache error logs
- 11.## Common Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Related Articles
- [Nginx troubleshooting: Fix Lambda Permission Denied - Complete ](fix-lambda-permission-denied)
- [Nginx web server troubleshooting: Fix Client Max Body Size Large Upload Nginx Issue ](client-max-body-size-large-upload-nginx)
- [Fix Apache LogLevel Core Debug Configuration](fix-apache-loglevel-core-debug)
- [Fix Cloudflare 502 Bad Gateway Error](fix-cloudflare-502-bad-gateway)
- [Fix Kong Gateway 500 Error](fix-kong-gateway-500-error)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Apache 502 Proxy Error", "description": "Step-by-step guide to fix Apache 502 Proxy Error. Resolve backend issues, configure mod_proxy, and fix timeout problems.", "url": "https://www.fixwikihub.com/fix-apache-502-proxy-error", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-27T10:35:00.000Z", "dateModified": "2026-04-27T10:35:00.000Z" } </script>