apache2 ignoring the default host and always redirecting to domain, even when using the IP - apache2

I am trying to make apache2 use a default site when a user access my server by IP address, instead of inputing the site's domain, just to tell the user he cannot access the ip directly.
What is happening is that it keeps redirecting to the domain's site, overwriting the IP address in the browser, instead of showing the default site.
Note the domain's site is redirected to https, this is expected.
The default site is not using ssl at all, and preferably should not use ssl.
My sites config are as folow (without logging, error documents nor ssl pathes directives):
000-default.conf
<VirtualHost *>
DocumentRoot /var/www/html
</VirtualHost>
example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/example.com/www
Redirect permanent / https://example.com
</VirtualHost>
example.com-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/example.com/www
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
All these sites are enabled and apache2 have been restarted.
> $ sudo apache2ctl -S
VirtualHost configuration:
*:* 1.2.3.4 (/etc/apache2/sites-enabled/000-default.conf:1)
*:443 example.com (/etc/apache2/sites-enabled/example.com-ssl.conf:2)
*:80 example.com (/etc/apache2/sites-enabled/example.com.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
http://example.com and https://example.com behave as expected.
http://1.2.3.4:80/ unexpectedly redirects to https://example.com.

You should avoid <virtualhost *> which amounts to <virtualhost *:*> which is really quite unusual. It is also a bad idea to omit ServerName from any virtualhost, because it will be plucked from the systems hostname and cause confusion.
Apache picks the best matching IP:PORT combination first, then looks at ServerName/ServerAlias from the set of virtualhosts with that best match.
This is why your *:80 virtualhost is selected -- it's a more specific match.

Related

Google Compute Engine Apache2 subdomain still directed a home instead of virtual host folder

I've been trying to create subdomains in my Google Cloud Console and then direct them to a folder in my /var/www directory.
I created a subdomain:
DNS Name: subdomain.example.com
Type: CNAME
TTL: 300
Data: example.com
I created a folder
/var/www/subdomain
I created a file with message "Subdomain!"
/var/www/subdomain/index.html
After, I duplicated my 000-default.conf
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/subdomain.conf
I edited the new file code to:
<VirtualHost *:80>
ServerAdmin webmaster#subdomain.example.com
DocumentRoot /var/www/subdomain
ServerName subdomain.example.com
ServerAlias www.subdomain.example.com
Redirect permanent / https://subdomain.example.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I then did
sudo a2ensite subdomain.conf
sudo service apache2 restart
Yet my subdomain is still directed at the document root /var/www/html
I noticed that when I do systemctl reload apache2 I get error Failed to connect to bus: No such file or directory
Did I miss a step or do I need to add something else?
The problem is that http is configured but not https.
Must add to default-ssl.conf
<VirtualHost *:443>
ServerName www.yoursite.com
DocumentRoot /var/www/site
SSLEngine on
SSLCertificateFile /path/to/www_yoursite_com.crt
SSLCertificateKeyFile /path/to/www_yoursite_com.key
SSLCertificateChainFile /path/to/DigiCertCA.crt
</VirtualHost>

Url and port redirection apache 2

My problem is that i configured my dns to redirect two urls towards my server url but one of them must contain the port.
exp
www.xxxx.com---> xxx.fff.fff.ggg
www.yyyy.com---->xxx.fff.fff.ggg:8080
but since dns don't accept port in the conf how should i configure the apache2 server to redirect the url to url:port?
thank you
Create name-based virtual host on port 80 (this is routine). In the virtualhost with ServerName www.yyyy.com, redirect all traffic to www.yyyy.com:8080.
# Only in 2.2:
NameVirtualHost *:80
<Virtualhost *:80>
ServerName www.xxxx.com
</virtualhost>
<Virtualhost *:80>
ServerName www.yyyy.com
Redirect / http://www.yyyy.com:8080/
</virtualhost>

Apache2 bad matching virtualhost

I have a little problem with virtualhosts.
My configuration is:
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/domains/site1.com
ServerName www.site1.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
If you enter the following URLs into browser:
https://www.site1.com - OK (matches VHost with /var/www/domains/site1.com DocumentRoot)
http://www.site2.com - OK (matches Vhost with /var/www/domains/site2.com DocumentRoot)
but when I try site2.com with https protocol (https://www.site2.com). Apache finds wrong Vhost with /var/www/domains/site1.com DocumentRoot..
Why?
Thanks for help.
It's because you only have HTTPS configured for site1 and not site2.
You'll need to duplicate the site1 VirtualHost configuration and apply site2's information to it so it'll look something like the one below:
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.site2.com
DocumentRoot /var/www/domains/site2.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
<VirtualHost *:443>
ServerName www.site1.com
DocumentRoot /var/www/domains/site1.com
SSLEngine on
SSLCertificateKeyFile /etc/apache2/ssl/....
SSLCertificateFile /etc/apache2/ssl/...
SSLCertificateChainFile /etc/apache2/ssl/...
</VirtualHost>
I'm assuming you have a separate SSL certificate for your other site or are just testing.
You may also run into the issue of hosting multiple domains with HTTPS with only one IP address on your server, in which you should follow the instructions here: http://wiki.apache.org/httpd/NameBasedSSLVHostsWithSNI
This is simply how virtual hosts work in Apache. The match for the IP:PORT combination at the TCP layer is checked first. If there are multiple virtual hosts with the "best match", then hostnames (from the HTTP Host: header) are checked.
In your simple case, that means the best match for a request on port 443 is the single *:443 vhost. It will never look at worse matches to find a matching hostname.

configurations for the Subdomain module in drupal 7

What are the appropriate configurations for the Subdomain module in drupal 7 on Window 7 running XAMPP?
My site is localhost/example
Here are the changes I have made:
settings.php
$cookie_domain = '';
Leaving this commented out gives me an error
"The $cookie_domain variable in settings.php is not set".
Uncommenting and putting in "localhost", ".localhost", "example", ".example" gives me an "Access denied" error.
It seems to accept "localhost/example" or a blank ' ', although I don't know if this is the right thing to do.
host
127.0.1.1 localhost example
(and I can successfully ping ideastar)
http.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example
ServerAlias *.example
DocumentRoot C:/xampp/htdocs
</VirtualHost>
getting the fallowing error
Subdomain error: localhost and subdomain-test.localhost did not resolve to the same IP address. Your DNS may be improperly configured and subdomains will likely not work.
First you should create two virtual host one for main domain and another for sub-domain. For ex: like we have two domains example.com and test.example.com to setup on local host. I am assuming that you have a fresh install of Xampp on your machine. Your virtual host [\xampp\apache\conf\extra\httpd-vhosts.conf] file should have these entries:
<VirtualHost *:80>
DocumentRoot "\xampp\htdocs\example"
ServerName example.com
ServerAlias example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "\xampp\htdocs\example"
ServerName test.example.com
ServerAlias test.example.com
</VirtualHost>
Now open you system host file and make the following entries below the line "127.0.1.1 localhost" (localhost ip 127.0.0.1)
127.0.1.1 example.com
127.0.1.1 test.example.com
\xampp\htdocs\example this directory should contain your Drupal setup.
Cookie domain entry should be as per the below line
$cookie_domain = '.example.com';
i have assumed that you already have a Drupal site running at http:\localhost\example.

ServerAlias www.example.com is not recognized

Below is my config file:
NameVirtualHost 12.34.56.78:80
<VirtualHost 12.34.56.78:80>
ServerAdmin admin#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /srv/www/domain1.com/public_html1/
ErrorLog /srv/www/domain1.com/logs/error.log
CustomLog /srv/www/domain1.com/logs/access.log combined
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerAdmin admin#domain2.com
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /srv/www/domain2.com/public_html1/
ErrorLog /srv/www/domain2.com/logs/error.log
CustomLog /srv/www/domain2.com/logs/access.log combined
</VirtualHost>
The thing is when I put www.domain1.com into browser, apache2 doesn't retrieve the web page resides in /srv/www/domain1.com/public_html1/, instead, it gets the page from the default document root defined in another file. However, if I put www.domain2.com, everything works fine. I don't see any difference between two VirtualHost config block, so I wonder what does make the difference. BTW, I haven't put any .htaccess file under their document root.
Try checking your Apache config by issuing:
/usr/sbin/apache2ctl -S
I guess your "default" virtual host uses "www.domain1.com" as its ServerName, thus it responds to the request to "www.domain1.com".
On my machine this happened due to "www.domain1.com" being my machine's hostname. If ServerName is not specified, Apache tries to guess - causing the abovementioned problem.
I solved this by specifying "ServerName default" in sites-available/default.
This might be of some help as well:
http://httpd.apache.org/docs/current/dns-caveats.html
If you are on Windows and probably using a ZendSever, than you MUST do next:
Open file "hosts" in directory "C:\Windows\System32\drivers\etc"
Add
127.0.0.1 www.domain1.com
under
127.0.0.1 localhost
127.0.0.1 domain1.com
It should look like this:
127.0.0.1 localhost
127.0.0.1 domain1.com
127.0.0.1 www.domain1.com
127.0.0.1 domain2.com
127.0.0.1 www.domain2.com
127.0.0.1 domain3.com
127.0.0.1 www.domain3.com
All domains need to be registered in this file.
Just wanted to say that this caught me out too, and changing the ServerName in the default apache site worked for me, as did simply disabling that site using
a2dissite default
Then reloading apache.

Resources