3

I Have two applications running on my server with domains

 x.abc.com
 admin.x.abc.com

x.abc.com is running on port 80
admin.x.abc.com is running on port 8080

I can able to access x.abc.com from browser, but when I try admin.x.abc.com, it is sending requests to x.abc.com only.

What is the proper way to configure two different web applications with different domain & port on single machine. Listed below my virtualhost files for x.abc.com & admin.x.abc.com respectively

<VirtualHost *:80>
    ServerName www.x.abc.com
    ServerAlias x.abc.com
    RailsEnv test
    DocumentRoot /home/cp/cp/public
   <Directory /home/cp/cp/public/>
   </Directory>
   LogLevel warn
   ErrorLog /home/cp/cp/log/error.log
   CustomLog /home/cp/cp/access.log combined
   RewriteCond %{HTTP_HOST} ^admin.x.abc.com
   RewriteRule (.*) http://localhost:8080/$1 [R=301,L]
</VirtualHost>

<VirtualHost *:8080>
    ServerName www.admin.x.abc.com
    ServerAlias admin.x.abc.com
    RailsEnv test
    DocumentRoot /home/cp/cp_admin/public
   <Directory /home/cp/cp_admin/public/>
   </Directory>
   LogLevel warn
   ErrorLog /home/cp/cp_admin/log/error.log
   CustomLog /home/cp/cp_admin/access.log combined
 </VirtualHost>
Colt
  • 1,939
  • 6
  • 20
  • 25
loganathan
  • 240
  • 2
  • 13

4 Answers4

1

You can add mutliple VirtualHost directives for a single port. Apache will process them according to the names, i.e. ServerNameand ServerAlias. Note that Apache defaults to the first VirtualHost configured, if no match is found. The following configuration should work.

<VirtualHost *:80>
    ServerName www.admin.x.abc.com
    ServerAlias admin.x.abc.com
    ...
</VirtualHost>

<VirtualHost *:80>
    ServerName www.x.abc.com
    ServerAlias x.abc.com
    ...
</VirtualHost>
M. Glatki
  • 1,868
  • 1
  • 16
  • 33
  • No it was not working, I have tried already – loganathan Apr 21 '16 at 06:26
  • Have you ensured that *both* hosts listen on port 80? – M. Glatki Apr 21 '16 at 12:11
  • 1
    The order of these two virtual hosts should perhaps be reversed to prevent `ServerAlias x.abc.com` matching `admin.x.abc.com`, as the [ServerAlias directive documentation](http://httpd.apache.org/docs/2.4/mod/core.html#serveralias) suggests would happen. – Colt Apr 21 '16 at 20:39
1

Your config seems to be good. Try the following:

  1. Are you using domains or IP? If it does not find the virtual host your are referring to, it will serve the first virtual host. In this case, www.x.abc.com. This is regardless of port.
  2. Try to add the Listen 8080 directive. You should have both Listen 80 and Listen 8080.
jarvis
  • 1,956
  • 4
  • 17
  • 31
0

It looks like what may be happening is that (1) all the traffic is being handled on port 80, and then (2) the ServerAlias x.abc.com is matching (and being used for) admin.x.abc.com. Note, from the Apache documentation regarding matching:

Name-based virtual hosts for the best-matching set of <virtualhost>s are processed in the order they appear in the configuration. The first matching ServerName or ServerAlias is used, with no different precedence for wildcards (nor for ServerName vs. ServerAlias).

Colt
  • 1,939
  • 6
  • 20
  • 25
-1

It is resolved after adding the below line in httpd.conf

NameVirtualHosts *:80
loganathan
  • 240
  • 2
  • 13
  • 1
    First, I assume that you mean `NameVirtualHost` as there is no `NameVirtualHosts` directive. Second, although the timing you describe may be correct, i.e. "it resolved AFTER adding the NameVirtualHost," this was probably not the CAUSE of the resolution. To be sure, the `NameVirtualHost` directive is deprecated in Apache 2.4. and, as noted in the [Apache documentation](http://httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost), "This directive currently has no effect." – Colt Apr 21 '16 at 17:48
  • @Colt It got fixed after adding this and FYI my Apache version is 2.2. Do you have any other solutions for this? – loganathan Apr 27 '16 at 06:45
  • Well, that changes about everything. Inaccurately tagging your question not only makes it very difficult for people to help you, but makes the question/answer confusing for people finding this thread in the future. I edited the tag from Apache 2.4 to Apache 2.2. The `NameVirtualHost` is required for Apache 2.2. – Colt Apr 27 '16 at 12:51