Why does my vhosts match without the domain name? - apache

I've configured my httpd-vhosts.conf file as follows:
<VirtualHost seg.localhost:81>
ServerAdmin my#email.com
DocumentRoot "D:\path\to\public_html"
ServerName http://seg.localhost
ServerAlias http://seg.localhost
ErrorLog "logs/seg.log"
CustomLog "logs/seg" common
<directory "D:\path\to\public_html">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
But when I go to http://localhost:81/ in my browser, it's still hitting that folder. Why is the subdomain ignored?

If you are using name based vhosts, the top-most vhost (the first instance of a <VirtualHost> block) is considered the "default" vhost, meaning that if a request is made for a host that doesn't match any of the given <VirtualHost>'s, the top-most one is used.
You can get around this by adding a new top-most vhost that simply denies everything:
<VirtualHost seg.localhost:81>
ServerName _default_
DocumentRoot "D:\path\to\public_html"
<Directory "D:\path\to\public_html">
Order Allow,Deny
Deny from all
</Directory>
</VirtualHost>
Or have it redirect to seg.localhost, or however you want to handle it.

Related

Redirect in one virtual host file affecting all

I have a virtual host file with a redirect in it to force requests for the domain to https.
It seems to work too well. I added another virtual host file, and all requests for it (:80) are redirected to the first domain on :443. That is, requests for sub.b.com:80 get redirected to a.com:443. Note that b.com is not on the same server, only its sub domain is.
a.com
<VirtualHost a.com:80>
ServerName a.com
Redirect permanent / https://a.com/
</VirtualHost>
<VirtualHost a.com:443>
ServerName a.com
DocumentRoot /var/www/html/a
SSLEngine On
SSLCertificateFile /usr/local/share/ca-certificates/www.a.com.crt
SSLCertificateKeyFile /etc/ssl/private/a.com.key
<Directory /var/www/html/a>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
sub.b.com
<VirtualHost *:80>
ServerName sub.b.com
DocumentRoot /var/www/html/b
<Directory /var/www/html/b>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Requests for b.com will be handled by the first <VirtualHost> because you have no <VirtualHost> for b.com.
You only have a <VirtualHost> for sub.b.com.
So b.com is handled by the first <VirtualHost> for that IP/port, since none is assigned to handle it and that is the way it defaults.

VirtualHost configuration issue

I have two apps I want to serve via port 80 in Apache on the same ip host. To do so, I have defined the following virtual hosts:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/var/www/wsgi/rest_api"
ServerName api
WSGIDaemonProcess rest_api user=gms threads=5
WSGIScriptAlias /api /var/www/wsgi/rest_api/rest_api.wsgi
WSGIPassAuthorization On
<Directory /var/www/wsgi/rest_api/rest_api>
Order deny,allow
Allow from all
Options +Indexes
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/extjs/cardiocatalogqt"
ServerName cardiocatalogqt
Alias /cardiocatalogqt /var/www/extjs/cardiocatalogqt
<Directory /var/www/extjs/cardiocatalogqt>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
The problem is that only the first one in the list is being recognized (they both work independently). What am I missing to get both of these working together, independent of order?
EDIT
I am trying to avoid use of different server names due to a CORS authentication issue across domains (which includes host names and ports). All I want is two different paths as such to resolve accordingly: http://test.com/cardiocatalogqt and http://test.com/api.
Please create two different virtual host with different server names and different document root paths
<VirtualHost *:80>
ServerAdmin admin#test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also, add the server name into the hosts file.
mod_alias was what I wanted, ala http://httpd.apache.org/docs/2.2/mod/mod_alias.html
Works like a charm!
EDIT
More specifically, my config looks like this:
WSGIDaemonProcess rest_api user=gms threads=5
WSGIScriptAlias /api /var/www/wsgi/rest_api/rest_api.wsgi
WSGIPassAuthorization On
<Directory /var/www/wsgi/rest_api/rest_api>
Order deny,allow
Allow from all
Options +Indexes
</Directory>
<VirtualHost *:80>
DocumentRoot "/var/www/extjs/cardiocatalogqt"
ServerName cardiocatalogqt
Alias /cardiocatalogqt /var/www/extjs/cardiocatalogqt
<Directory /var/www/extjs/cardiocatalogqt>
Options Indexes FollowSymLinks
AllowOverride None
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>

Two apps on Apache server with URI

I need to install a server with Apache 2.2 on Linux and I need to do two VirtualHosts differentiated by URI.
But with only one domain name and one ip address. And I can't use Alias.
I tried something like that but that doesn't work :
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName localhost/app1
ServerAlias www.localhost/app1
<Directory /var/www/app1>
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/app2
ServerName localhost/app2
ServerAlias www.localhost/app2
<Directory /var/www/app2>
Allow from all
</Directory>
</VirtualHost>
Thank you to the first answer here, it's working : https://serverfault.com/questions/588841/two-apps-on-apache-server-with-uri
I put the answer here if one day the link doesn't work :
What you could do is set up a reverse proxy to different virtual hosts listening only on loopback.
You would get in your www.localhost virtualhost:
<VirtualHost *:80>
DocumentRoot /var/www/
ServerName localhost
ServerAlias www.localhost
ProxyPassReverse /app1/ http://webapp1.local/
ProxyPassReverse /app2/ http://webapp2.local/
</Virtualhost>
And create two virtualhosts for the apps:
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/app1
ServerName webapp1.local
<Directory /var/www/app1>
Allow from all
</Directory>
</Virtualhost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/app2
ServerName webapp2.local
<Directory /var/www/app2>
Allow from all
</Directory>
</Virtualhost>
Make sure to add webapp1.local and webapp2.local to your /etc/hosts file.
Since you have only a single domain name and only a single ip address available there is no means for the apache server to distinguish which host is meant. Therefore there is noo sense in defining VirtualHosts here.
However you certainly can place two apps in separate folders inside your DocumentRoot:
ServerName whatever-your-domain.is
DocumentRoot /var/www
<Directory /var/www/app1>
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/app2>
Order allow,deny
Allow from all
</Directory>
Then you'd call those apps by their paths:
apps1: http://whatever-your-domain.is/app1
apps2: http://whatever-your-domain.is/app2
Don't forget to take care of requests to the "main folder" of that single host: /var/www which can be reached by http://whatever-your-domain.is/

Why does port-based VirtualHost not work?

I'm trying to serve up two different Django apps on two separate ports. I have app1 on port 80 and app2 on port 8080. So what I'm expecting is that when I put example.com:80 into my browser, my request should get served up from app1, and when I put example.com:8080 into my browser, my request should get served up from app2. However, sometimes it will serve up my request from the wrong app. Then, after a few refreshes, it will sometimes switch. What's more, sometimes it will serve up from the correct app and then, after a few refreshes, switch to the wrong one. I know this isn't due to caching on the browser's end because I've also tried sending requests with curl from the machine and I get the same behavior. I can't understand why I'm getting this unexpected, nondeterministic behavior.
Here is my VirtualHost configuration:
<VirtualHost *:80>
ServerAlias *
ServerPath /app1/
WSGIScriptAlias /app1 /home/eyuelt/app1/app1/wsgi.py
Alias /app1/static /home/eyuelt/app1/staticfiles
<Directory /home/eyuelt/app1/app1>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/eyuelt/app1/staticfiles>
AllowOverride None
Order Deny,Allow
Require all granted
</Directory>
</VirtualHost>
Listen 8080
<VirtualHost *:8080>
ServerAlias *
ServerPath /app2/
WSGIScriptAlias /app2 /home/eyuelt/app2/app2/wsgi.py
Alias /app2/static /home/eyuelt/app2/staticfiles
<Directory /home/eyuelt/app2/app2>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
<Directory /home/eyuelt/app2/staticfiles>
AllowOverride None
Order Deny,Allow
Require all granted
</Directory>
</VirtualHost>
I suspect this might be the problem:
http://httpd.apache.org/docs/2.2/vhosts/examples.html
This is what you want: You have multiple domains going to the same IP
and also want to serve multiple ports. By defining the ports in the
"NameVirtualHost" tag, you can allow this to work.
One or both of these could be the problem: If you try using
without the NameVirtualHost name:port or you
try to use the Listen directive, your configuration will not work.
Server example:
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
ServerName www.example.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 172.20.30.40:80>
ServerName www.example.org
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

Two sites with Apache virtual hosts

I'm working on 2 different sites. I have local copies of them checked out from SVN at my home folder, and symbolic links in /var/www pointing to them.
I've set up virtual hosts in httpd.conf like this:
<VirtualHost *:80>
DocumentRoot /var/www/siteA
ServerName 192.168.0.10/siteA
<Directory "/var/www/siteA">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/siteB
ServerName 192.168.0.10/siteB
<Directory "/var/www/siteB">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I would like to be able to access the sites with 192.168.0.10/siteA and /siteB, but when typing the address in browser, it throws a 404 and apache error log says: "File does not exist: /var/www/siteA/siteA".
What am I doing wrong and where does the second "/siteA" come from?
You've got too much configuration in there. /SiteA and /SiteB should be paths relative to your site root, you can't have different sites on the same host header, which in this case is "192.168.0.10". If you want to bind that address to the configuration, something along the lines of the following should work.
<VirtualHost 192.168.0.10:80>
DocumentRoot /var/www
ServerName 192.168.0.10
<Directory "/var/www">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
If you now browse to /SiteA, /SiteB you should see the contents of your directory as long as your symlinks are correct. You also need to ensure you have the "NameVirtualHost" directive set correctly in your httpd.conf.