When working with development environments or multi-service architectures, it's common to need subdomain-based routing to different ports on the same server. The key requirements are:
- Maintaining the original subdomain in the browser address bar
- Transparently serving content from a different port
- Working within Apache 2.2's configuration system
Before configuring Apache, ensure your DNS is properly set up:
dev.mydomain.com. IN A 192.168.1.100
*.mydomain.com. IN A 192.168.1.100
Here's the complete solution for your Apache 2.2 configuration:
<VirtualHost *:80>
ServerName dev.mydomain.com
ServerAlias *.mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog ${APACHE_LOG_DIR}/dev-error.log
CustomLog ${APACHE_LOG_DIR}/dev-access.log combined
</VirtualHost>
For more complex scenarios, you might prefer using mod_rewrite:
<VirtualHost *:80>
ServerName dev.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dev\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}:8080$1 [P,L]
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
Make sure these modules are enabled:
a2enmod proxy
a2enmod proxy_http
a2enmod rewrite
After configuration, test with:
sudo apache2ctl configtest
sudo service apache2 reload
Common issues to check:
- Firewall blocking port 8080
- SELinux policies (if applicable)
- Missing trailing slashes in ProxyPass directives
For handling multiple subdomains:
<VirtualHost *:80>
ServerName dev.mydomain.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
<VirtualHost *:80>
ServerName api.mydomain.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
<VirtualHost *:80>
ServerName db.mydomain.com
ProxyPass / http://localhost:8081/
ProxyPassReverse / http://localhost:8081/
</VirtualHost>
Even for internal networks, basic security measures should be implemented:
<Proxy *>
Order deny,allow
Allow from 192.168.1.0/24
Deny from all
</Proxy>
When you need to route dev.mydomain.com
to mydomain.com:8080
while maintaining the original subdomain in the browser's address bar, you're essentially looking for a reverse proxy solution. This is particularly common in development environments where services run on different ports but should be accessible via standard HTTP ports (80/443) with clean URLs.
Before configuring Apache, ensure your DNS is properly set up:
dev.mydomain.com. IN A 192.168.1.100
mydomain.com. IN A 192.168.1.100
Both records should point to the same server IP address where Apache is running.
Here's the essential configuration for Apache 2.2:
<VirtualHost *:80>
ServerName dev.mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog ${APACHE_LOG_DIR}/dev_error.log
CustomLog ${APACHE_LOG_DIR}/dev_access.log combined
</VirtualHost>
ProxyPreserveHost: Maintains the original Host header from the client request
ProxyPass: Maps incoming requests to the backend server
ProxyPassReverse: Adjusts headers in backend responses
Ensure these modules are enabled:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart
For more complex scenarios, you might need additional directives:
<Location />
ProxyPass http://localhost:8080/ timeout=300 keepalive=On
ProxyPassReverse http://localhost:8080/
ProxyPassReverseCookieDomain localhost dev.mydomain.com
ProxyPassReverseCookiePath / /
</Location>
If you encounter 502 errors, check:
- The backend service is running on port 8080
- No firewall is blocking internal connections
- SELinux contexts are properly set (for RHEL/CentOS)
While you mentioned this is for an intranet, basic security measures are still recommended:
<Proxy *>
Order deny,allow
Allow from 192.168.1.0/24
</Proxy>