Using mod proxy to pass requests to local servers transparently
Home > CentOS > CentOS 6.x > Apache web server configuration > Using mod proxy to pass requests to local servers transparently
We can use 'ProxyPass' and 'ReverseProxyPass' apache configuration directives to pass requests coming to virtual host or to given path at virtual host to another web server. For example if we have yaws web server listening on localhost:8888 then we can use following configuration to pass all requests sent to 'http://server/tools/' to to to http://localhost:8888/ internally.
ProxyPass /tools/ http://127.0.0.1:8888/ ProxyPassReverse /tools/ http://127.0.0.1:8888/
Please note that because requests are being forwarded local server will not get correct access logs and machine itself will appear as client in all requests. However apache server will get correct logs with correct client IPs for URLs http://server/tools/*
Note that we can pass the information of originating server using directive
ProxyVia On
When we use this apache will put IP address of original client in 'X-Forwarded-For' HTTP request header while proxying request so that ProxyPass destination machine can make decision based on actual clients, and even log it.
In case the port number and URL on server are different then few things may not work. For example:
<VirtualHost *:80> ServerAdmin rekall@example.com #DocumentRoot /var/www/html ServerName server1.sbarjatiya.com ErrorLog logs/server1.sbarjatiya.com-error_log CustomLog logs/server1.sbarjatiya.com-access_log combined <Location /> ProxyPass http://server1.sbarjatiya.com:8080/app1/ ProxyPassReverse http://server1.sbarjatiya.com:8080/app1/ </Location> </VirtualHost>
creates issues with images and various links by appending an unnecessary /app1 on most links. However if we use following configuration:
<VirtualHost *:80> ServerAdmin rekall@example.com DocumentRoot /var/www/server1 ServerName server1.sbarjatiya.com ErrorLog logs/server1.sbarjatiya.com-error_log CustomLog logs/server1.sbarjatiya.com-access_log combined ProxyPass /app1/ http://server1.sbarjatiya.com:8080/app1/ ProxyPassReverse /app1/ http://server1.sbarjatiya.com:8080/app1/ </VirtualHost>
after creating a redirect from / to /app1/ using HTTP-META refresh used in /var/www/server1/index.html the pages open properly with all images and links working as expected.
Home > CentOS > CentOS 6.x > Apache web server configuration > Using mod proxy to pass requests to local servers transparently