Apache Server + Docker Reverse Proxy Setup Guide (Production)
This guide provides step-by-step instructions for setting up Apache Server as a reverse proxy for AIV running in Docker containers in a production environment.
1. Architecture
Internet -> Apache (80/443) -> Docker (127.0.0.1:8080) -> App (/aiv)
2. Install
Update your system and install required packages:
sudo apt update
sudo apt install apache2 docker.io docker-compose certbot python3-certbot-apache -y
Enable modules
sudo a2enmod proxy proxy_http rewrite ssl headers
sudo systemctl restart apache2
3. Application Configuration
To ensure the application correctly interprets forwarded headers, please add the following property in your <location>/aiv/repository/econfig/application.yml:
server:
forward-headers-strategy: native
4. DNS Check
Check domain resolution:
nslookup xxxx.xxxx.com
OR
dig xxxx.xxxx.com
Expected:
Server IP = YOUR SERVER PUBLIC IP
5. Apache Config
Create the Apache configuration file at /etc/apache2/sites-available/aiv.conf:
<VirtualHost *:80>
ServerName xxxx.xxxx.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ProxyPreserveHost On
ProxyPass /aiv/ http://127.0.0.1:8080/aiv/
ProxyPassReverse /aiv/ http://127.0.0.1:8080/aiv/
</VirtualHost>
<VirtualHost *:443>
ServerName xxxx.xxxx.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/xxxx.xxxx.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxxx.xxxx.com/privkey.pem
Header always set X-Forwarded-Proto "https"
ProxyPreserveHost On
ProxyPass /aiv/ http://127.0.0.1:8080/aiv/
ProxyPassReverse /aiv/ http://127.0.0.1:8080/aiv/
</VirtualHost>
6. Apache Site Management
Disable default sites
sudo a2dissite 000-default.conf
sudo a2dissite 000-default-le-ssl.conf
Enable app site
sudo a2ensite aiv.conf
sudo systemctl restart apache2
7. SSL Setup
Obtain SSL certificate:
sudo certbot --apache -d xxxx.xxxx.com
Test auto-renewal:
sudo certbot renew --dry-run
8. Verification Commands
Apache status
sudo systemctl status apache2
Ports
sudo ss -tulpn | grep -E ':80|:443'
Backend
curl http://127.0.0.1:8080/aiv/
Public
https://xxxx.xxxx.com/aiv/
9. Common Issues & Fixes
(1) 404 Error
Check:
curl http://127.0.0.1:8080/aiv/
Fix:
- Ensure ProxyPass includes /aiv/
- Ensure app supports /aiv base path
- Restart Apache:
sudo systemctl restart apache2
(2) SSL Error / HTTPS not working
Check:
sudo certbot certificates
Fix:
sudo certbot --apache -d xxxxx.xxxx.com
sudo systemctl restart apache2
(3) Port Conflict (80/443 busy)
Check:
sudo ss -tulpn | grep :80
sudo ss -tulpn | grep :443
Fix:
- Stop Docker exposing ports 80/443
- Ensure only Apache uses 80/443
- Restart:
sudo systemctl restart apache2
(4) Mixed Content Error
Check browser console:
http:// calls inside https page
Fix:
- Replace all API URLs:
http://xxxx.xxxx.com -> https://xxxx.xxxx.com - Add Apache header:
Header always set X-Forwarded-Proto "https"
10. Final Working Flow
User -> https://xxxx.xxxx.com/aiv/
Apache -> Proxy
Docker -> http://127.0.0.1:8080/aiv/
App -> Response