On this page
- When is Apache a good choice as a proxy?
- How do you enable the proxy modules?
- How do you write the proxy virtual host?
- How do you pass WebSocket connections?
- How do you keep static files and some paths local?
- How do you add HTTPS and the right forwarded headers?
- How do you balance several app instances?
- How do you troubleshoot the proxy?
- Questions
When is Apache a good choice as a proxy?
Apache works well as a proxy when it already serves other sites on the server, when you rely on .htaccess rules for a PHP app next door, or when your team knows its config style. The proxy modules ship with every Apache build, so there is nothing extra to download. Apache forwards each matching request to the app, rewrites redirect headers coming back, and handles TLS for the domain.
Start with a working Apache install from the Apache guide and an app that answers on the server itself, for example curl http://127.0.0.1:3000/. Keep the app bound to 127.0.0.1 so the port is not reachable from outside.
How do you enable the proxy modules?
On Ubuntu and Debian, turn on the modules you need with a2enmod. proxy_wstunnel is only required on older releases but does no harm:
sudo a2enmod proxy proxy_http proxy_wstunnel headers ssl remoteip
sudo systemctl restart apache2
apache2ctl -M | grep proxyOn Rocky Linux and AlmaLinux, /etc/httpd/conf.modules.d/00-proxy.conf already loads mod_proxy and its helpers. Confirm with httpd -M | grep proxy. SELinux there blocks Apache from opening network connections by default, so allow it once:
sudo setsebool -P httpd_can_network_connect 1
getsebool httpd_can_network_connectSkipping that step is the most common reason for a 503 on Rocky and Alma, with an AVC entry in /var/log/audit/audit.log.
How do you write the proxy virtual host?
Create a virtual host for the app name. On Debian family use /etc/apache2/sites-available/app.example.net.conf; on Rocky and Alma use /etc/httpd/conf.d/app.example.net.conf and change the log paths to /var/log/httpd.
<VirtualHost *:80>
ServerName app.example.net
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/ timeout=120
ProxyPassReverse / http://127.0.0.1:3000/
ErrorLog ${APACHE_LOG_DIR}/app-error.log
CustomLog ${APACHE_LOG_DIR}/app-access.log combined
</VirtualHost>ProxyRequests Off keeps Apache from acting as an open forward proxy. ProxyPreserveHost On passes the original host name to the app. ProxyPassReverse rewrites Location headers in redirects, so the browser never sees the internal port. mod_proxy adds X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Server on its own.
sudo a2ensite app.example.net.conf # Debian family
sudo apachectl configtest && sudo systemctl reload apache2 # or httpd
curl -I http://app.example.net/How do you pass WebSocket connections?
Apache 2.4.47 and newer can upgrade connections inside mod_proxy_http, which every supported release in this guide ships. Add the upgrade parameter to the rule:
ProxyPass / http://127.0.0.1:3000/ upgrade=websocket timeout=3600
ProxyPassReverse / http://127.0.0.1:3000/The long timeout keeps quiet sockets open. If only one path carries WebSockets, such as /socket.io/, put a separate rule for it above the general one with the upgrade parameter and leave the main rule with a normal timeout. Rules are matched in the order they appear, so specific paths always go first.
How do you keep static files and some paths local?
Exclude a path from proxying with an exclamation mark and let Apache read it from disk. The exclusion has to come before the catch-all rule:
Alias /static/ /var/www/app/static/
<Directory /var/www/app/static/>
Require all granted
</Directory>
ProxyPass /static/ !
ProxyPass /.well-known/acme-challenge/ !
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/Excluding the ACME path is useful if you later run Certbot in webroot mode. You can also proxy only part of a site, for example ProxyPass /api/ http://127.0.0.1:8000/, while the rest of the virtual host keeps serving PHP files.
How do you add HTTPS and the right forwarded headers?
Run Certbot with the Apache plugin; it copies your proxy rules into a new TLS virtual host.
sudo apt install -y certbot python3-certbot-apache # Ubuntu and Debian
sudo dnf install -y epel-release && sudo dnf install -y certbot python3-certbot-apache # Rocky and Alma
sudo certbot --apache -d app.example.net
sudo certbot renew --dry-runmod_proxy does not send the original scheme on its own, so add it in the TLS host (the -le-ssl.conf file on Debian family):
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"Then configure the app to trust one proxy hop so it builds HTTPS links. More certificate options are in the Certbot guide, and choosing between www and the bare domain is covered in HTTPS and www redirects.
How do you balance several app instances?
mod_proxy_balancer spreads requests over several members defined in a Proxy block. Enable it on Debian family with sudo a2enmod proxy_balancer lbmethod_byrequests slotmem_shm; Rocky and Alma load these by default.
<Proxy "balancer://appcluster">
BalancerMember http://127.0.0.1:3001 retry=30
BalancerMember http://127.0.0.1:3002 retry=30
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / balancer://appcluster/
ProxyPassReverse / balancer://appcluster/lbmethod=bybusyness sends work to the member with fewest active requests, and bytraffic balances by bytes. Members can also be other servers by public IP; in that case restrict their app port to this server in their firewall and encrypt the traffic, as explained in the Nginx load balancing guide.
How do you troubleshoot the proxy?
Read the virtual host error log first; mod_proxy messages start with codes that point to the cause.
sudo tail -n 30 /var/log/apache2/app-error.log # /var/log/httpd/ on Rocky and Alma
curl -I http://127.0.0.1:3000/
sudo apachectl -S- AH01114 failed to make connection to backend: the app is down, on another port, or SELinux is blocking the connection.
- No protocol handler was valid:
proxy_httpis not loaded. - 504 or AH01102 timeout: raise
timeout=on the ProxyPass line orProxyTimeout. - Redirects point to 127.0.0.1:3000: the
ProxyPassReverseline is missing or does not match. - Wrong virtual host answers:
apachectl -Slists which names map to which file.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Apache as a reverse proxy
Do I need ProxyPassReverse?
Yes, whenever the app sends redirects. It rewrites the Location header so the browser goes to your public name instead of the internal address.
Is ProxyRequests On ever needed for a reverse proxy?
No. That setting turns Apache into a forward proxy for any site. Keep it off for reverse proxying.
How do I see the real visitor IP in the app?
Read the X-Forwarded-For header and enable the framework trusted proxy option. Apache adds that header automatically.
Can Apache proxy to a Unix socket?
Yes, with a rule such as ProxyPass / unix:/run/app/app.sock|http://localhost/. Apache needs read and write permission on the socket.
Should I use Nginx instead?
Both do the job. Use Apache when it already serves other sites on the server; see the Nginx reverse proxy guide for the alternative.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Compression with gzip and Brotli
- HTTPS and www redirects
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site