limit apache virtualhost to one domain? - apache

I have configuration of virtualhost:
<VirtualHost xx.xx.xx.xxx>
ServerName mydomain.com
ServerAlias *
...
</VirtualHost>
everything works fine on mydomain.com. But the problem is that someone linked his domain to my application ip and I want to avoid this. Is it possible to limit virtualhost to only one domain "mydomain.com" - so if somebody will point his domain to my ip - virtualhost will not process this request?

There's no way to prevent it from answering HTTP requests on that IP address. I think that if you were to use name based hosting and then create a vhost with ServerName 1.2.3.4 then it'd be matched for requests to the IP address and you'd be able to add some special behaviour there (return an error code, redirect to the named site, etc).
If you just want to make sure that people don't bookmark the IP address and always see mydomain.com in the browser then have a look at the section on Canonical Hostnames in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Related

Configuring apache virtual hosts for one domain and multiple IP addresses

I have two apache servers at two separate IP addresses and one domain name (example.com) administer on godaddy.com. I want to use this single domain to point traffic to my two separate servers over ssl.
I set up the first server through a fios router with port forwarding and associated the public IP address with a DDNS address (xxx.ddns.net). My godaddy config looks like this:
Type Name Value
-------------------------------
A # 184.168.131.241
CNAME www xxx.ddns.net
Forwarding
------------
domain https://www.example.com
... and my apache ssl.conf file looks like this:
<VirtualHost *:443>
...
ServerName www.example.com
DocumentRoot /var/www/html
</VirtualHost>
This works fine, but now I want to set up a subdomain ('mysub') so that traffic directed to mysub.example.com gets handled by my second server available at a separate IP address (MY.2ND.IP.ADDR). I've tried all sorts of settings on godaddy and in the ssl.conf file, but to no avail. In particular, I've tried:
Setting godaddy so that traffic sent to subdomain mysub.example.com is redirected straight to MY.2ND.IP.ADDR; it's not at all clear to me if this is possible. (Godaddy has a form under the title "Forwarding" and a subsection therein called "subdomain", but setting that subdomain to mysub and setting "forward to" to http://MY.2ND.IP.ADDR doesn't seem to do anything; what is this 'subdomain' field in the godaddy interface supposed to be used for?)
Directing all traffic to the first server, and configuring apache to proxy traffic for the subdomain on to the second server.
I've tried adding another virtual host element to ssl.conf like so:
<VirtualHost MY.2ND.IP.ADDR:443>
ServerName mysub.example.com
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://MY.2ND.IP.ADDR
ProxyPassReverse http://MY.2ND.IP.ADDR
</Location>
</VirtualHost>
... but this doesn't work either.
In summary, what is the best way to direct traffic for a subdomain to a separate IP address using godaddy and/or apache configurations?

Apache multihost does not respect server name

I have two django applications on one machine. I would like to run them on the same address and port. So the user should be point to the correct address according the request domain/host. My current apache.conf file looks like:
<VirtualHost *:80>
ServerName 35.232.14.xxx
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.my_testing_domain.com
...
</VirtualHost>
Note that there is real IP address and real domain and other stuff required by Django app. I build the file according to the documentation: https://httpd.apache.org/docs/2.4/vhosts/examples.html#page-header (apache multiple sites on one IP) and https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 (django with apache).
The thing is, that every request goes to the first VirtualHost (in this case the IP address). If I switch their order in the apache.conf, then every request goes to the domain VirtualHost.
So one of the sites is working correctly (always the first one).
Why the apache does not respect the server names? What am I missing?
I add Server alias withou "www":
ServerAlias my_testing_domain.com
to the "domain based" virtual host. Now it works.

How to make a Permanent redirect to an internal ip address with apache in Ubuntu?

I have the following problem: I want to redirect any request received on the external ip lets say 192.168.x.x, to an internal ip of the same machine, lets say 172.0.0.1. The internal ip has been associated to a name editing the hosts file as follows:
$/etc/hosts
...
...
172.0.0.1 www.example.com
I want to have www.example.com in the HTTP_HOST field of the request header unregarding to which external ip has been requested (In order to have django sites working properly without adding the current ip every time to the site list). Consequently I tried the following:
$/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:443>
ServerName entrypoint.com
Redirect 301 / https://www.example.com:443
</VirtualHost>
<VirtualHost 172.0.0.1:443>
ServerName www.example.com
...
...
</VirtualHost>
When I try to connect from a browser the redirect seems to look for a web domain instead of the local ip. How can I fix this?

Why should we create A DNS record for subdomains when we can do it via httpd.conf?

I want to implement a blog in my site with usernames as subdomains like user1.domain.com.
I've read bunch of articles that was mentioned to create an A record like below in DNS:
*.domain.com A 127.0.0.1 ;OR you site ip address
Then use .htaccess to redirect requests from subdomains to a folder in your server.
But I find another approach that was said to just modify your httpd.conf as below:
<VirtualHost *:80>
DocumentRoot /abs/path/to/webroot
ServerName domainname.com
ServerAlias *.domainname.com
<Directory /abs/path/to/webroot>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Then retrieve subdomains via $_SERVER['HTTP_HOST'].
The question is why should someone use htaccess and put pressure on apache and create A DNS record when we can do a simple thing like ServerAlias *.domainname.com in httpd.conf?
I appreciate in advance for your views.
You need to setup both a wildcard DNS record and configure a server alias in httpd.conf to enable the hosting of arbitrary sub domains. They configure completely different things.
Add the wildcard DNS records is what allows clients to determine the IP address of your sub domain. With the wildcard DNS record about user1.domain.com will resolve to 127.0.0.1. If you do not have a DNS record no user1.domain.com will not be a valid name and it will not resolve to an IP address. the DNS record is to allow clients to connect to your server.
The httpd.conf configuration is local to your Apache server. It does not affect clients. The line ServerAlias *.domainname.com will configure Apache to tell it to process the arbitrary sub domains. Client's will not be directly affected by this setting.
You have added the ServerAlias, but for your computer to find that user1.domain.com and user2.domain.com should resolve to 127.0.0.1, you need to update your HOSTS file or DNS Table.
The configuration you have written is for Apache Server for handling the redirects when the user requests the domain. So, essentially, either you need a Dynamic HOSTS file or a service which resolves *.domain.com to your 127.0.0.1 server.
Hope this helps. :)

IP based VirtualHost and Apache

I have this webserver that have an IP address xxx.xxx.xx.x, I also have a website I want to publish, but I do not have any domain for my website yet.
So in my httpd-vhosts.conf file I have this setting:
<VirtualHost xxx.xxx.xx.x>
ServerName xxx.xxx.xx.x
DocumentRoot "C:\Sites\mysite"
</VirtualHost>
And since I dont have a domain I really want to use the IP address to reach my site, but I have tried this and it does not work. I guess you HAVE to set a server name in ServerName as the title says.
Are there any ways for me to make my website public through my IP address, if yes how can I do this?
Try
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot C:\Sites\mysite
ServerName xx.xx.xx.xx
</VirtualHost>
Remember to restart apache,
You may also need to add,
Listen xx.xx.xx.xx:80
If you only have the one website on this server, you don't need a virtual host. Just set the DocumentRoot correctly and away you go. Also make sure Apache is listening on all IP addresses (Listen 0.0.0.0:80.)
If that doesn't work for you, from your command prompt do:
telnet xx.xx.xx.xx 80
GET /
and see what you get back - you should get your website's default page.
This is not a programming question.
But anyway,
Set the VirtualHost to * rather than a specific IP address. I don't think you need the servername either then.