How to fix: mod_proxy’s ProxyPass directive does not work
10th February 2016
So… You had finally built a nice LXC container for your web-facing application, and even configured Apache (Debian package version 2.14.18-1 in my case) to serve some static/web-only components.
From your client-side JavaScript UI you talk (in JSON) to the API, which is implemented as a separate node.js/Python/etc server – say, on port 8000 in the same LXC container.
The simplest solution to forward requests from the web-frontend to your API is by using mod_proxy.
If you want to forward any requests to /api/* to your custom back-end server on port 8000, you just add the following lines to your VirtualHost configuration:
ProxyPass “/api” “http://localhost:8000″
ProxyPassReverse “/api” “http://localhost:8000″
I’d suggest not wrapping this fragment with the classical IfModule: as your application will not really work without its API back-end, you actually want Apache to fail as soon as possible if mod_proxy is missing.
That was easy, right? What, it doesn’t work? Can’t be! It’s dead simple! No way you could make a mistake in 2 lines of configuration!!! :mad_rage:
Oh wait… I remember I had this problem before…
Let’s check:
- Step 1. Did you disable (using
a2dissite default
ora2dissite 000-default
, depending on your Debian-based GNU/Linux) the default website? If your application and the default website are configured in a similar way, then it might be the default site which is serving your app’s pages. The most sure way is to just disable it. - Step 2. Did you enable also the proxy_http sub-module? (Using
a2enmod proxy_http
, followed byservice apache2 restart
) mod_proxy is only the core module, actual per-protocol work is done by these sub-modules.
Your requests to /api should now be passed on to your API server. If not – please write in the comments what was the problem in your case and how you solved it. HTH!