mod_proxy apache vhosts.conf - apache

sorry for my bad English, I'm gonna try to explain my problem.... I have to do a configuration of Apache for school. I want create a web server model with three host. I have three virtual machine on virtual box and each one can communicate with an internal network. Indeed three different apache server can be seen in each vm if I call it in browser.
Now I have to configure mod_proxy.
I want this configuration: the first vm is a server, responding a specifical domain, from this server I want to reach the other 2 apache from the other 2 different vm. Server localhost ip address 192.168.1.100 vm01 localhost/vm01 link to ip address 192.168.1.101 vm02 localhost/vm02 link to ip address 192.168.1.102
So, I spend few days in apache mod_proxy but I can't find a perfect guide or example.
I try to use this vhosts.conf in server, but didn't work. Please be patient I'm new in Apache.
<VirtualHost *:8080>
ServerName localhost
DocumentRoot /home/francesco/proxy/htdocs/
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin webmaster#proxy.com
ServerName www.vm01.com
ProxyPass /vm01 http://192.168.1.101
ProxyPassReverse /vm01 http://192.168.1.101
</VirtualHost>

You have defined two Virtual Hosts on port 8080. Combine it to one.
<VirtualHost *:8080>
ServerAdmin webmaster#proxy.com
ServerName www.vm01.com
ProxyPreserveHost On
ProxyPass /vm01 http://192.168.1.101
ProxyPassReverse /vm01 http://192.168.1.101
</VirtualHost>

Related

How can I set up a load balancer for multiple virtual hosts (apache)

I am trying to set up a load balancer for a couple of virtual hosts on my apache server.
These virtual hosts are added by adding the following lines for the file "C:\Windows\System32\drivers\etc\hosts":
127.0.0.1 localhost
127.0.0.1 vhosta
127.0.0.1 vhostb
127.0.0.1 vhostc
127.0.0.1 load-balancer
::1 localhost
Then I've added the following lines for the file "C:\xampp\apache\conf\extra\httpd-vhosts.conf":
<VirtualHost *:80>
DocumentRoot c:/xampp/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhosta
ServerName vhosta
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhostb
ServerName vhostb
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhostc
ServerName vhostc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/load-balancer
ServerName load-balancer
</VirtualHost>
And of course I've created the folders in C:/vhosts/ and added an index.php file to each one (with an echo statement inside).
Now, I can access the virtual hosts through my browser by visiting "http://vhosta" etc.
But what I need, is to make a load balancer that chooses to execute either "http://vhosta", "http://vhostb" or "http://vhostc".
How can I achieve this? And have I done everything correct so far?
Any help will be greatly appreciated!
Thanks in advance!
(i am using xampp on windows 8.1 btw.)
There many ways to accomplish this, but what you are trying to do it won't work. The /etc/hosts is a basic way of IP to host name mapping. If you want round-robin resolution you will have to use DNS server. Also, it doesn't make much sense to load balance on the same machine, except for learning and configuration testing.
These are some of the options you have.
1) Using mod_proxy_balancer. You need to enable mod_proxy and mod_proxy_balancer modules. Also, you need to pick one of the scheduler algorithms. Options are: mod_lbmethod_byrequests, mod_lbmethod_bytraffic, mod_lbmethod_bybusyness and mod_lbmethod_heartbeat.
http://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html
<VirtualHost *:80>
...
ServerName load-balancer
<Proxy balancer://mybalancers>
BalancerMember http://vhosta:80
BalancerMember http://vhostb:80
BalancerMember http://vhostc:80
</Proxy>
ProxyPass / balancer://mybalancers
ProxyPassReverse / balancer://mybalancers
...
</VirtualHost>
2) Using DNS round-robin option. You need to point multiple IPs to the same host name. With this option, when you make a request to your load-balancer host, DNS server will give you next IP (in a round-robin fashion).
DNS configuration
load-balancer IN A 10.0.0.1
load-balancer IN A 10.0.0.2
load-balancer IN A 10.0.0.3
Virtual hosts for apache servers
<VirtualHost 10.0.0.1:80>
DocumentRoot c:/vhosts/vhosta
ServerName load-balancer
</VirtualHost>
<VirtualHost 10.0.0.2:80>
DocumentRoot c:/vhosts/vhostb
ServerName load-balancer
</VirtualHost>
<VirtualHost 10.0.0.3:80>
DocumentRoot c:/vhosts/vhostc
ServerName load-balancer
</VirtualHost>
And one more thing related to hosts file. If you want to map a loopback IP to hostname, feel free use full range, from 127.0.0.0 to 127.255.255.255. I'm pretty sure this is mapped in Windows, but I have no ways to test it. To test, just ping 127.1.2.3, and see what you get back.
http://en.wikipedia.org/wiki/Loopback
This is how you can organize your /etc/hosts file if you need multiple IPs for testing.
127.0.0.1 localhost
127.0.0.2 vhosta
127.0.0.3 vhostb
127.0.0.4 vhostc
127.0.0.5 load-balancer

How to customise my URL which is having IP Address

Is it possible to configure my URL which has my IP address on it- like: "http://192.168.xx.yy/index.php". The situation is when I run Apache server in my PC, and load localhost in it. I know it is possible after hosting with external server, but is there any way we can configure within our localhost?
How to configure the Apache files to make this happen? I tried in my localhost, editing the "httpd.conf" by adding this inside like this - please tell me where I am getting the issue!
ServerName localhost:80
HostnameLookups Off
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot /www/otherdomain
</VirtualHost>
DocumentRoot "c:/wamp/www/"
Yes, you can play with multiple IP addresses on your machine. Configuration depends on your OS. Article Create Multiple IP Addresses to One Single Network Interface is for linux.
But, better way is to use VirtualHosts based on host names or (simplest) on ports. So you can get http://siteA.mycoputer.localhost, http://siteB.mycomputer.localhost in the first case and http://192.168.x.y:8000, http://192.168.x.y:9000 in the second case
Here is Apache Server config example from Apache Server 2.2 documentation
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here
</VirtualHost>

NamedVirtualHost in apache configuration

I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml

Apache mod_proxy - Pass through from localhost to another domain?

I'm not at all experienced when using proxies but hopefully this is possible.
I have apache running on my development machine which hosts a website (www.testdomain.com). I need to look at www.testdomain.com from my phone. I don't want to root my phone so all I can do is enter the IP address of my machine (10.8.0.1). The site hosted at www.testdomain.com won't work correctly as the default site in apache.
I need some way of passing through a request to 10.8.0.1 to www.testdomain.com without my phone having to look up the DNS record for www.testdomanin.com
Is this possible using mod_proxy? Is there something else that will do the job?
First of all, I don't really understand why can't you point your cellphone browser to www.testdomain.com.
Anyway, you could use a proxy, but I think a better approach would be to use a ServerAlias directive in your www.testdomain.com vhost:
<VirtualHost *:80>
ServerName www.testdomain.com
ServerAlias 10.8.0.1
(...)
If you still want to use a proxy, you could setup a different vhost for 10.8.0.1:
<VirtualHost *:80>
ServerName 10.8.0.1
ErrorLog ...
TransferLog ...
LogLevel warn
# ReverseProxy
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://www.testdomain.com/
ProxyPassReverse / http://www.testdomain.com/
(...)
ref: http://httpd.apache.org/docs/current/vhosts/name-based.html

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/