apache redirection to niginx based on domain name - apache

I have got APP1 running on apache httd over port 80, which will be reached via app1.abc.com , and I have got another app running on nginx over 8080 port on the same server which can be reached via app2.abc.com:8080.
Infact both app1.abc.com and app2.abc.com mapped to same IP x.x.x.1. So now I would like to know if I can configure some redirect rule in apache like, when traffic comes from app2.abc.com, with out 8080 port specified I should be internally routing this to nginx over 8080 port based on the domain that I came from.
Any help on this would be greatly helpful.
Update 1
I have tried this apache reverse proxy config.
ServerName app1.subdomain.domain.com
ServerAlias app1-app
ProxyPass / http://0.0.0.0:80/
ProxyPassReverse / http://0.0.0.0:80/
</VirtualHost>
<VirtualHost *:80>
ServerName app11.app1.subdomain.domain.com
ServerAlias vpstage-target
ProxyPass / http://0.0.0.0:8080/
ProxyPassReverse / http://0.0.0.0:8080/
</VirtualHost>

Related

What is the correct way of having apache redirect to https AND tomcat (port 8080) at the same time

I know similar questions have been asked a lot already, and I feel like I read all of them 12 times. Every time the answer is slightly different, and I tried virtually all combinations, but still cannot get it to work...
So, I have an Apache and a Tomcat running in a Freenas Jail (so running FreeBSD). I used Certbot to get an SSL certificate for my domain. Lets call that example.com. In my router, I opened ports 80 and 443.
Now, I want users to just enter either 'www.example.com' or 'example.com' in their browser, and land on 'https://www.example.com' and port 8080.
I found that to accomplish this, I need to configure my apache Virtual Hosts file. However, as I said, I found many different things to put in there, and none of them seem to be exactly the right ones. Here is what I have now:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# ProxyPreserveHost On
# ProxyRequests Off
# ProxyPass / http://localhost:8080/
# ProxyPassReverse / http://localhost:8080/
# Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost _default_:443>
SSLEngine on
SSLCertificateFile /usr/local/etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /usr/local/etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /usr/local/etc/letsencrypt/live/example.com/chain.pem
ServerName www.example.com
ServerAlias example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
I also added this into the Tomcat server.xml:
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8080"
proxyName="www.example.com"
proxyPort="80"/>
So, my questions are these:
Does it matter which one has www, ServerName or ServerAlias? Because I've seen both.-
Should I have Apache listen on port 80 or 433?
How can I verify if Apache and tomcat are listening on the right ports? Before, I had this in a CentOS VPS, and there it was with 'netstat -tulpn'. (I didnt have SSL yet back then)
At one point, I had it almost working: entering example.com was properly redirected to https://www.example.com/ on port 8080 because I reached the website runnning in Tomcat. If I removed then the 's' in the URL, it did not redirect to https again. Also, I should have saved that exact config because I cannot find it back...
I will be so thankful for any answer I get. Thanks a lot in advance.
Reygok
Let's go through your questions:
Does it matter which one has www, ServerName or ServerAlias?
Use in server name the canonical hostname, in alias aliases pointing to your CNAME. Choose which name you want to advertise to the users.
Should I have Apache listen on port 80 or 443?
You must do both because Let's Encrypt requires port 80 to be open, so HTTPd has to do Listen *:80 and Listen *:443.
How can I verify if Apache and tomcat are listening on the right ports?
FreeBSD magic: sockstat -46
Now to your setup:
Assumptions: HTTPd and Tomcat run on the same host and Tomcat listens on localhost.
Tomcat's server.xml:
<Connector address="localhost" port="8080" redirectPort="443" ... />
I never needed the proxy* attributes, just used this in the <Host />:
<Valve className="org.apache.catalina.valves.RemoteIpValve" />
the access log valve will require: requestAttributesEnabled="true"
HTTPd:
<VirtualHost *:80>
Redirect permanent / https://{hostname}/
</VirtualHost>
<VirtualHost *:443>
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
RequestHeader set X-Forwarded-Proto "https"
</VirtualHost>
In your web.xml you set to have Tomcat to redirect to HTTP to HTTPS: http://docs.adaptivecomputing.com/viewpoint/hpc/Content/topics/1-setup/securityConfiguration/modifyingWebxmlEnableHTTPS.htm

Using Joomla on Apache behind Apache Reverse Proxy

I have a Joomla Installation on Apache2 (Port 83) running on Ubuntu 16.04 Server.
When I connect to the page from the local network like 192.168.178.14:83 it appears and everything is fine. To access my page from the Internet there is another Server running Apache2, that acts as a Proxy Server. All traffic going trough Port 80 and 443 come to this machine, using the ServerName it decide to which machine the traffic have to be forwarded. That's working fine for about 10 domains and machines.
But when I connect to my Joomla Domain I get a HTML Site without any formatting, graphics and templates. When I forward a Port directly to this machine everything works fine again, using this port to connect.
network topologie
My VirtualHost config on Ubuntu1:
<VirtualHost *:80>
ServerName ksg-events.de
Redirect permanent / https://ksg-events.de/
</VirtualHost>
<VirtualHost *:443>
ServerName ksg-events.de
ProxyPreserveHost On
ProxyRequests Off
ProxyVia Off
ProxyPass / http://192.168.178.14:83/
ProxyPassReverse / http://192.168.178.14:83/
SSLCertificateFile /etc/letsencrypt/live/ksg-events.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ksg-events.de/privkey.pem
</VirtualHost>
Virtual Host config on Ubuntu2:
<VirtualHost *:83>
DocumentRoot /var/www/ksg-events.de/joomla
</VirtualHost>
Thanks for your help!
I find a solution to my problem:
Run Joomla behind Apache reverse proxy (http):
Add ProxyVia full to proxy's VirtualHost config
Using https:
Enable mod_headers on proxy server
Add RequestHeader set X-Forwarded-Proto "https" to proxy's VirtualHost
I hope that can help someone who run into the same problems.

Apache 2.2 and tomcat redirection for subdomains

I have two tomcat servers running in my machine and i want to use apache to redirect the traffic to each server depending on the subdomain.
My httpd.conf is:
<VirtualHost *:80>
ServerName dev.bo.example.com
ProxyPass / ajp://localhost:11009/bo/
ProxyPassReverse / ajp://localhost:11009/bo/
ProxyPassReverseCookiePath /bo/ http://localhost:11001/bo/
</VirtualHost>
<VirtualHost *:80>
ServerName dev.com.example.com
ProxyPass / ajp://localhost:10009/com/
ProxyPassReverse / ajp://localhost:10009/com/
ProxyPassReverseCookiePath /com/ http://localhost:10001/com/
</VirtualHost>
The problem is that the traffic is always direct to the first tomcat server despite the subdomain i use.
It seems that only the first virtual host match despite if i use:
http://dev.com.com.example.com/ or
http://dev.bo.example.com/
Does any know what i am missing here?
Finally, i found that i missed configuration i had to add
NameVirtualHost *:80
to the httpd.conf

Apache host header proxy

I have multiple urls coming into a server. I want to user host headers to redirect the traffic. I am trying to use Apache to redirect these requests to various servers that are inside our firewall. I have gotten part of the solution, but, I seem to be missing something.
For example, http://hostHeader1.mycompany.com should be redirected to a server inside our firewall that handles requests for hostHeader1, and the result should be handed back to the client. http://hostHeader2.mycompany.com should be redirected to a server inside our firewall that handles requests for hostHeader2. Etc.
Right now, I have the following, but, it redirects all traffic to http://hostHeader1Handler/:
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://hostHeader1Handler/
ProxyPassReverse / http://hostHeader1Handler/
ServerName hostHeader1.mycompany.com
</VirtualHost>
Any help appreciated.
Scott
This is probably your first or your only virtual host. Just add another virtual host before. Then this should be the new default.
NameVirtualHost *:*
<VirtualHost *:*>
ServerName your.default.domain.de
DocumentRoot /var/www/pathToHTML
</VirtualHost>
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://hostHeader1Handler/
ProxyPassReverse / http://hostHeader1Handler/
ServerName hostHeader1.mycompany.com
</VirtualHost>

How to redirect different sub domain requests to different port

I have two applications, one is the www.myexample.com, another is the blog.myexample.com. I am using PHP and Apache.
Now, I want to let www.myexample.com runs on port 82 of my machine, and blog.myexample.com on port 83, on the same machine. How to configure the apache and/ or the PHP scripts so that when the requests for the requests are served properly?
Edit: Thanks for everyone who responds, but I afraid I don't get the question clear-- my bad!
What I really want is to simulate a condition whereby the www.myexample.com and blog.myexample.com are located on different machines. So when a request comes in, the gateway server ( the one which is also hosting www.myexample.com) will check whether this is a request for www.myexample.com or for blog.myexample.com and does the necessary reroutes.
How to do this? Thanks.
I will assume that you have your own reason for wanting the two sites (www and blog) to run on different ports - and in different processes. If this is not what you intended, e.g. you did not want to have two distinct processes, then having different ports may not be what you intended either: use VirtualHost instead, to co-host the two domains within the same apache+php instance on port 80. Otherwise, read on.
Assuming that you have your two apache+php processes listening on localhost:82 and localhost:83 respectively, bring up a third, apache-only process to act as a reverse proxy. Have the reverse proxy apache instance listen for requests coming on port 80 from the internet, with two virtual host definitions. The first virtual host definition, www, would forward requests to localhost:82, whereas the second virtual host definition, blog, would forward requests to locahost:83, e.g.:
NameVirtualHost *:80
# www
<VirtualHost *:80>
ServerName www.myexample.com
ProxyPass / http://localhost:82/
ProxyPassReverse / http://localhost:82/
</VirtualHost>
# blog
<VirtualHost *:80>
ServerName blog.myexample.com
ProxyPass / http://localhost:83/
ProxyPassReverse / http://localhost:83/
</VirtualHost>
I use proxy for this type of things.
In my example, I have apache 1.3 running on port 80, but I needed svn repository to run on apache 2.2, and I didn't want to type :82 on the end of the domain every time. So I made proxy redirection on apache 1.3 (port 80):
<VirtualHost *:80>
ServerName svn.mydomain.com
ServerAlias svn
ServerAdmin my#email.com
<IfModule mod_proxy.c>
ProxyPass / http://svn:82/
</IfModule>
</VirtualHost>
Run the following line on terminal (specify your domain and sub domain name correctly)
sudo nano /etc/apache2/sites-available/subdomain.domain.com.conf
Paste the following code and change as your requirement
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerName subdomain.domain.com
ServerAlias subdomain.domain.com
ProxyRequests Off
#ProxyPass / http://localhost:8080/
<Location />
ProxyPreserveHost On
ProxyPass http://domain.com:8080/
ProxyPassReverse http://domain.com:8080/
</Location>
# Uncomment the line below if your site uses SSL.
#SSLProxyEngine On
</VirtualHost>
Run the following lines on terminal (specify your domain and sub domain name correctly)
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod subdomain.domain.com.conf
sudo service apache2 restart
Off the top of my hat:
Listen 82
Listen 83
NameVirtualHost 1.2.3.4 # Use your server's IP here
<VirtualHost www.myexample.com:82>
# Configure www.myexample.com here
</VirtualHost>
<VirtualHost blog.myexample.com:83>
# Configure blog.myexample.com here
</VirtualHost>
A more complete answer to this would be to do something like this which allow you to setup a proxy gateway which is what is loosly described above.
ServerName localhost
<Proxy *>
Order deny,allow
Allow from localhost
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:10081/
ProxyPassReverse / http://localhost:10081/
ProxyPassReverseCookiePath / http://localhost:10081/