How can I use apache or nginx as frontend for a node app? - apache

I'm trying to develop a Node.js web application, but my production environment-to-be is already hosting Apache/2.2.22. So I can't have Node use port 80, and I don't want my users to have to go to http://myapp.com:4000/.
Is there an apache module that does this, perhaps like mod_jk does this for Tomcat?
The same question goes for nginx.

mod_proxy can do that (for apache)
<VirtualHost nodejs.host.com>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:4000
ProxyPassReverse / http://localhost:4000
</VirtualHost>
will forward everything on that virtualhost to Node.js

Related

How do I setup an Apache ProxyPass / Reverse Proxy while listening on an alternate port?

I had Apache configuration which was using a ProxyPass/ProxyPassReverse with the following example syntax:
ProxyPass /myprog http://localhost:8080/myprog
ProxyPassReverse /myprog http://localhost:8080/myprog
Then, I moved Apache from port 80, to 8880, and put another web server on port 80. I can access some things correctly now on port 8880 - those files which are hosted directly on Apache. But my proxy pass (to Tomcat) now fails when I try to access it at: http://some.domain:8880/myprog.
How do I correct the ProxyPass/Reseverse to account for the port change? (I assumed, perhaps natively, the port spec was implicit...).
You need to put your configuration on a VirtualHost tag.
<VirtualHost *:8880>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /myprog http://localhost:8080/myprog
ProxyPassReverse /myprog http://localhost:8080/myprog
</VirtualHost>

Easy configuration for domain:8080 to 80 Tomcat

Is there an easy way to change from domain.com:8080/myapp to domain.com? Any step by step working examples for Tomcat 7?
Yes, you can use Apache Mod_Proxy for this. You use Apache2 as a front-end to your Tomcat instance.
Here is a configuration example:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName site.domain.com
Redirect / /tomcat-context/
ProxyPass /tomcat-context/ http://127.0.0.1:tomcat-port/tomcat-context/
ProxyPassReverse /tomcat-context/ http://site.domain.com/tomcat-context/
ProxyPreserveHost On
</VirtualHost>
You can find more infos here.
It may seem a bit hard to grasp at first if you're not an Apache2 expert (I am not), but once you've configured your first frontend, adding more and more tomcat instances behind it is a breeze.
Or if you're lazy, change the connector port from 8080 to 80 in the conf/server.xml file in your tomcat home directory.

What are my options to deploy Go applications alongside PHP applications?

What I'm basically trying to accomplish is having my main website running a CMS written in Go. This will be located at www.example.com.
I also have applications written in PHP located in directories, such as www.example.com/clients/
How can I serve example.com/clients using Apache/PHP while serving example.com using Go built-in web server?
Via mod_proxy in Apache2, you can proxy different paths into different destinations at localhost or anywhere else accessible by your server, including within your local network (if your server can access it).
For this you would use ProxyPass (Apache2 Docs for ProxyPass, which is very useful reading) like the example below:
<VirtualHost *:80>
ServerName some.example.host.xyz
DocumentRoot /var/www/your-document-root
Alias /clients/ /var/www/clients/
ProxyPass /clients/ !
ScriptAlias /something-using-cgi/ /var/www/cgi-stuff/
ProxyPass /something-using-cgi/ !
ProxyPreserveHost On
ProxyPass / http://localhost:9876/
ProxyPassReverse / http://localhost:9876/
ProxyPass /elsewhere/ http://elsewhere.example.host.xyz:1234/
ProxyPassReverse /elsewhere/ http://elsewhere.example.host.xyz:1234/
</VirtualHost>
You'll want to be sure that you set your proxy security such that external users can't use your reverse proxy as a forward proxy, too. You can do that via ProxyRequests as described in the official Apache2 docs. The way I did this on a server is to put this in your server-wide config (you should verify on your own that this is secure enough):
# disables forward proxy
ProxyRequests Off
Andrew Gerrand has a good blog post about this for nginx but the principle is the same for Apache.
You want to set up Apache as a reverse proxy for requests coming in for the Go application.
For Apache you want to look at mod_proxy

Apache - Tomcat ProxyPass VirtualHost - Context Path

I have a problem configuring apache tomcat ProxyPass directive for two applications that have two different Context Paths in tomcat. The tomcat is running behind an apache and I use the apache to proxy path the requests to tomcat. In apache I want to access both application via a hostname instead of a context path.
Scenario:
tomcat
https://domain:8443/app1
https://domain:8443/app2
in tomcat the applications have the context path app1 and app2
in apache I want to enable both application as follow:
https://app1.host/
https://app2.host/
In apache I have created a configuration for each domain:
ProxyPass / https://localhost:8443/app1
ProxyPassReverse / https://localhost:/8443/app1
The strange thing is app1 is only available through apache using the context path:
https://app1.host/app1
Is it possible to realize such a setup with apache ProxyPass module?
Thx for your help.
You should be able to achieve the result you want by using virtual hosting. Also it's a good idea to pass the requests to tomcat via the AJP protocol instead of HTTPS. Try adding this to the Apache configuration
NameVirtualHost *:443
<VirtualHost *:443>
ServerName app1.host
ProxyPass / ajp://localhost:8009/app1/
</VirtualHost>
<VirtualHost *:443>
ServerName app2.host
ProxyPass / ajp://localhost:8009/app2/
</VirtualHost>
If you haven't changed the default server settings for Tomcat this should work just as it is. Otherwise make sure to specify the AJP port that is configured in Tomcat's conf/server.xml file. There should be a line similar to this:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Make sure that you have the mod_proxy and mod_proxy_ajp modules loaded in Apache configuration, this may vary depending on your Apache installation. Also remove any previously configured 'ProxyPass / ...' lines as they will interfere with the new configuration. Hope this works for you.
you can try
ProxyPass / https://localhost:8443/app1/
ProxyPassReverse / https://localhost:8443/app1/
with the final /

Apache VirtualHost with mod-proxy and SSL

I am trying to setup a server with multiple web applications which will all be served through apache VirtualHost (apache running on the same server). My main constrain is that each web application must use SSL encryption. After googling for a while and looking other questions on stackoverflow, I wrote the following configuration for the VirtualHost:
<VirtualHost 1.2.3.4:443>
ServerName host.domain.org
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
SSLProxyEngine On
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / https://localhost:8443/
ProxyPassReverse / https://localhost:8443/
</VirtualHost>
Even though https://host.domain.org:8443 is accessible, https://host.domain.org is not, which defeats the purpose of my virtual host configuration. Firefox complains that even though it successfully connected to the server, the connection was interrupted. Chrome return an error 107: net::ERR_SSL_PROTOCOL_ERROR.
Finally I should also mention that the virtual host works perfectly fine when I do not use SSL.
How can I make this work ?
Thanks
You don't need to configure SSL in both Apache and Tomcat.
The easiest way to accomplish that is configure SSL just on Apache and proxy to tomcat using http.