Apache Virtual Host - Mod Proxy issues - apache

Not too au fait with Apache, have setup simple virtual hosts before without an issue. Seem to be having a bizarre one, working with 1 IP address and multiple servers.
My second virtual host seems to only look at the first 2 entries and ignores the rest. I have mapped out what I want to happen and how my vhost file looks and the second image is what is happening.
How my virtual hosts are setup and envision the routing to act
How it's currently operating
Now it maybe not right using virtual hosts on both servers?
Not too au fait with Apache, have setup simple virtual hosts before without an issue. Seem to be having a bizarre one, working with 1 IP address and multiple servers.
My second virtual host seems to only look at the first 2 entries and ignores the rest. I have mapped out what I want to happen and how my vhost file looks and the second image is what is happening.
Server1
<VirtualHost *:80>
ServerName server1.domain.com
ServerAlias x.domain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias y.domain.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://server2
ProxyPassReverse / http://server2
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias z.domain.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://server2
ProxyPassReverse / http://server2
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias w.domain.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://server2
ProxyPassReverse / http://server2
</VirtualHost>
Server 2
<VirtualHost *:80>
ServerName server2.domain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias z.domain.com
DocumentRoot /var/www/html/z
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias w.domain.com
DocumentRoot /var/www/html/w
</VirtualHost>
<VirtualHost *:80>
ServerName server2.domain.com
ServerAlias y.domain.com
DocumentRoot /var/www/html/y
</VirtualHost>

Avoid giving the same Servername to any two VirtualHost entries. Using x,y,z, etc names is perfectly ok (assumed all resolve to the one IP address from external users).
For simplicity (of maintenance and managing the configuration) also avoid mixing "standard" server configuration (e.g. x on server1) with VirtualHost ones. Any accessible area on your web namespace should be configured via VirtualHost. (The first one in your configuration becomming "default" in that case.)
Using VirtualHosts on both servers is not a problem by itself. You just need to ensure the ServerName from the original HTTP request is correctly being forwarded and arriving at server2. For this you need to add
ProxyPreserveHost On
to your VirtualHost configurations.
It might, however, be easier to give up the name based VirtualHosts usage with server2 and turn to using different ip address or ports for the individual (logical) hosts. Server2 is (at least by concept) an internal setting and not visible to the "external" side. Thus, ease of use is not truly attributable

Related

Apache Virtual Hosts serves the vhost NOT matched

I set up an Apache Server with two Virtual Hosts and it has a very weird behaviour. I have a normal webserver, that should be server in all cases, except given the case, that the domain name is "biblio.name" or "biblio-intra.name", which should be redirected to a virtual machine located on my laptop, serving another webservice on Linux. On my laptop I use xampp for the Apache Server. I have the following "httpd-vhosts.conf" in my apache/conf/extra folder:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName sis.name/
ServerAlias *
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
ServerName biblio.name/
ServerAlias biblio-intra.name/
</VirtualHost>
So I expected it to redirect all requests to "biblio.name" and serve the rest as normal. However, it didn't!
When I enter my static ip-adress (assigned from the router) in the browser, I get served htdocs as normal, perfectly fine. When I enter biblio.name:8080 I also get normally served the virtual machine as expected (obviously, since 8080 automatically uses the redirect rule of the virtual machine.) However, when I type "sis.name" it redirects me to the virtual machine and when I type "biblio.name" it serves me from the htdocs.
I never experienced a behaviour like that and I don't get, why it serves the opposite host of the one supposed to.
Am I missing something?
For some reason I don't know (anymore), all my apache vhost configurations have the same value for ServerName and ServerAlias. Also the / in it seems odd. You can try listing biblio-intra.name as second option, but it should at first repeat the value from ServerName:
<VirtualHost *:80>
ServerName sis.name
ServerAlias sis.name
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName biblio.name
ServerAlias biblio.name biblio-intra.name
ProxyPreserveHost On
ProxyRequests Off
ProxyPass "/" "http://127.0.0.1:8080/"
ProxyPassReverse "/" "http://127.0.0.1:8080/"
</VirtualHost>

Spring Boot Project work with Apache Http Server

I have two project that write with spring boot, and have separate port number.
server.port: 23100
server.port: 23101
now, I want to deploy the two project into one server and shard the same port 80,
How should I do to support this case ?
I know it can use Apache Http Server to support PHP etc. how to do this will Spring Boot ?
Update
thanks #HeadBangingSloth give this solution, general idea is to redirect 80 port to local port number according to domain name.
create vhost.conf file in /etc/httpd/conf.d/ folder
add following content according to your real case.
restart http server via service httpd restart
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.app1.com
ServerAlias app1.com
ProxyPass / http://localhost:23100/
ProxyPassReverse / http://localhost:23100/
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.app2.com
ServerAlias app2.com
ProxyPass / http://localhost:23101/
ProxyPassReverse / http://localhost:23101/
</VirtualHost>
I would suggest looking at this question here Apache redirect to another port
If you deploy your applications to your sever, you can set up your VirtualHosts in Apache to pass the incoming connections along
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.app1.com
ServerAlias app1.com
ProxyPass / http://localhost:23100/
ProxyPassReverse / http://localhost:23100/
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName www.app2.com
ServerAlias app2.com
ProxyPass / http://localhost:23101/
ProxyPassReverse / http://localhost:23101/
</VirtualHost>

Using VirtualHosts to host multiple domains

I have searched far and wide for this answer, and can't find a working solution. So here goes,
I have 2 VirtualHosts set up on my server, with each serving a separate domain name. However, when I visit the first domain on the list, it then serves the DocumentRoot of the second domain. I even have them both listening on different ports. In my DNS under each domain I've got them leading to the IP of the server.
Here is my Apache .conf file:
ServerName 137.117.33.226
<VirtualHost *:443>
ServerName joshstroup.me
ServerAlias www.joshstroup.me
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/joshstroup.me.cert
SSLCertificateKeyFile /etc/apache2/ssl/joshstroup.me.key
</VirtualHost>
<VirtualHost *:80>
ServerName respice.xyz
ServerAlias www.respice.xyz
DocumentRoot /var/www/respice
</VirtualHost>

Two servers with mod_proxy, second server host is local ip and shows /proxy/ in url?

I am having some issues in regards to sessions with a second server I am running on my home network. I do this as a hobby and to develop new applications before they officially go live.
I have a domain pointing to my ip and resolves successfully to server 1, but after configuring mod_proxy to send specific domains to server 2 I am getting some unwanted errors and results. I want the second server to act as a normal server and just go through the first server since my current router can only send port 80 to one local ip and not filter it.
I have a.mydomain.com for my second server and it resolves fine but When I try to use a web application on this second server I get the following error
Warning: You are now accessing Mydomain from http://10.0.1.38/, but Mydomain has been configured to run at this address: http://a.mydomain.com/
Can i fix this?
Also when trying to access phpmyadmin via the a.mydomain.com/phpmyadmin it will change to a.mydomain.com/proxy/phpmyadmin after logging in, can i change this so that it's basically seamless and does not add /proxy.
Here is my vhost config for server 1
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.server1domain.net
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ServerName a.mydomain.com
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://10.0.1.38/
ProxyPassReverse / http://10.0.1.38/
</VirtualHost>
Here is the vhost config for server 2
<VirtualHost *:80>
ServerAdmin admin#mydomain.com
DocumentRoot /var/www/mydomaincom
ServerName a.mydomain.com
</VirtualHost>
I am running Centos 6.4
Alright I finally figured this out, some of the stuff is a little obvious now but this works for anyone else in a similar situation.
So earlier in my http.conf I had this
<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# destination directory
ProxyPass /proxy http://10.0.1.38
ProxyPassReverse /proxy http://10.0.1.38
</IfModule>
There seems to be two problems with this, it seems to add the /proxy/ directory and also is wrong since the ip does not have a trailing slash, thus I changed it to this
<IfModule mod_proxy.c>
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</IfModule>
And my http.conf vhost config for the first server looks like this now
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://10.0.1.38/
ServerName a.mydomain.com
</VirtualHost>
If you forget the trailing slash after the ip you will most likely end up with 502 errors: Could not resolve dns
The second servers vhost config looks like this
<VirtualHost *:80>
ServerAdmin admin#mydomain.com
DocumentRoot /var/www/mydomain
ServerName a.mydomain.com
</VirtualHost>
Hope that helps anyone else with similar issues.

Virtual Host points to another Virtual Host

HELP! I just setup a virtual host for two sites that have a lot of traffic and I think I just messed something up! Here is the end of my httpd.conf:
NameVirtualHost *
<VirtualHost *>
ServerName www.mydomain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *>
ServerName www.mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
<VirtualHost *>
ServerName mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
I added the last virtual host to solve the problem of mydomain2.com going to wwww.mydomain.com. HOWEVER, what has happened now is that www.mydomain2.com goes to www.mydomain.com.
Please help!!!
Thanks all
UPDATE
STUPIDITY beyond words - managed to copy one site to two directories and hence the 2 domains pointing to the same place!! OMG this will not happen again. Double check and recheck and recheck and recheck and recheck and recheck........
Btw, why would someone neg rep me for this?
Instead of adding the third virtual host, add
ServerAlias mydomain2.com
to the second one. So your entire configuration would be basically this:
NameVirtualHost *
<VirtualHost *>
ServerName www.mydomain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *>
ServerName www.mydomain2.com
ServerAlias mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
If you want requests for mydomain.com to actually be redirected to www.mydomain.com, so that the user sees the URL change in his/her browser, that can be done with mod_rewrite (but that's the subject of another question, search for it if you like)
I was also having problems with this it turns out that for ServerName Apache did not like the www prepended. So it should look like this:
<VirtualHost *>
ServerName mydomain2.com
ServerAlias www.mydomain2.com *.mydomain.com
DocumentRoot "c:/wamp/www" #WAMP INSTALL
</VirtualHost>