Search code examples
node.jsapache.htaccesscontent-typemod-proxy

Apache, Node, .htaccess


I have an apache webserver listening on port 80. With apache, a PHP/MySQL system based on Zend framework. And I also have a node server listening on port 3000.

When a client sends a request, always on port 80, it's therefore first handled by apache. I would like to apply the following rules before treating the request:

if content-type is "application/json" then
    use apache web server
else if content-type is "application/zend" then
    use apache web server
else
    use node server

Here content-type is sent in the request headers. Content-type "application/zend" is a custom content-type to say that, for this type of particular request, we don't want to use node server (I need this for some reasons).

I've tried to modify httpd-vhosts.conf with

ProxyPreserveHost on
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

But that's of course not enough as not everything should be handled by the node server (listening on port 3000). Hence some rules should be added. But I'm unsure where/how. I also tried to change the .htaccess file, but not sure how either.

Any help would be great! Thanks!


Solution

  • This should work (in .conf file):

    RewriteEngine on
    RewriteCond %{HTTP:Content-type} !=application/json
    RewriteCond %{HTTP:Content-type} !=application/zend
    RewriteRule ^ http://%{HTTP_HOST}:3000%{REQUEST_URI} [P]
    

    Keep in mind that this might carry a performance penalty, and if most of your requests end on node, we should perhaps search for better solution.