Apache serving name-based virtual Host from server hostname (but shouldn't be) - apache

I have an Apache httpd server (2.4.6) running on Centos, serving several Name-based virtual hosts. The fqdn / hostname of the server should be serving no content, but it redirects to one of the Name-based virtual hosts and I don't understand why, or how to stop it.
In the configs below, a request to http://host.mydomain.org/ is redirected to http://www.customer.co.uk/.
Any ideas as to why, and the right way to stop it appretiated.
My httpd.conf is:
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root#xxx.org
ServerName host.mydomain.org:80
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all denied
</Directory>
<Directory "/var/www/html">
Options None
AllowOverride None
Require all denied
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all denied
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .cgi
AddHandler cgi-script .pl
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset ISO-8859-1
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
TraceEnable Off
ServerTokens Prod
IncludeOptional conf.d/*.conf
An example virtual host is:
<VirtualHost *:80>
DocumentRoot /home/xxx/customer/docs
ServerName www.customer.co.uk
ErrorLog /home/xxx/customer/logs/error_log
CustomLog /home/xxx/customer/logs/access_log combined
<Directory /home/xxx/customer/docs>
AllowOverride None
Options None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName customer.co.uk
Redirect permanent / http://www.customer.co.uk/
</VirtualHost>
<VirtualHost *:80>
ServerName www.customer.mydomain.org
Redirect permanent / http://www.customer.co.uk/
</VirtualHost>
Thanks very much
Kevin

The first VirtualHost in the configuration file has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName or ServerAlias directives, it will be served by this first <VirtualHost>.
You can prevent this by adding this (for example) as the first VirtualHost:
<VirtualHost *:80>
ServerName default
RewriteEngine On
RewriteRule ^ - [F]
</VirtualHost>

Related

504 Error AWS Server gateway timeout after apache(httpd) configuration with mod_wsgi for django app in redhat

I got an error called Gateway Timeout when trying to load my server elastic ip. I changed documents root to /var/www/public_html My OS is Redhat AWS EC2 Instance
My projects root is
/var/www/public_html
/var/www/public_html/main
/var/www/public_html/main/wsgi.py
When i Run this command to check Configuration File Error
sudo httpd -t
Result is
[Tue Oct 05 01:32:39.954924 2021] [so:warn] [pid 417878:tid 139824059787584] AH01574: module wsgi_module is already loaded, skipping
Syntax OK
I am Using python3-mod_wsgi
Error is
[wsgi:error] [pid 415061:tid 140537755555584] Timeout when reading response headers from daemon process 'main': /var/www/public_html/main/wsgi.py
My virtual host configuration file /etc/httpd/sites-available/vhosts.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
#ServerAdmin webmaster#localhost
DocumentRoot /var/www/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog /var/log/httpd/3.109.245.1-error.log
CustomLog /var/log/httpd/3.109.245.1-access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Alias /static /var/www/public_html/static
<Directory /var/www/public_html/static>
Require all granted
</Directory>
<Directory /var/www/public_html/main>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIPassAuthorization On
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess main python-path=/var/www/public_html/main/ python-home=/var/www/public_html/venv
WSGIProcessGroup main
WSGIScriptAlias / /var/www/public_html/main/wsgi.py
My httpd(apache) configuration file httpd.conf
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
IncludeOptional sites-enabled/*.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGIApplicationGroup %{GLOBAL}
My wsgi.py
"""
WSGI config for main project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
import sys
from django.core.wsgi import get_wsgi_application
#sys.path.append('/var/www/public_html/main/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
application = get_wsgi_application()

Apache stopping all servers when one has a problem

I am relatively new to Apache and am having a problem where all virutal servers are being stopped when one of them has a problem. We had a problem which brought down our backend service (testing.my-domain-name.ch) for testing and caused a 502 error, and apache delivered that same error for our production facility (www.my-domain-name.ch), even though it was still running (verified by using a wget).
My httpd.conf file looks like:
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
Listen 0.0.0.0:80
Include conf.modules.d/*.conf # Everything in conf.d is at default values
User apache
Group apache
ServerAdmin root#localhost
ServerName test.my-domain-name.ch:80
UseCanonicalName Off
#
<Directory />
Options FollowSymLinks
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
#
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf # Everything in conf.d is at default values
<VirtualHost *:80>
ServerName my-domain-name.ch
Redirect permanent / https://www.my-domain-name.ch/
</VirtualHost>
<VirtualHost *:80>
ServerName production.my-domain-name.ch
ServerAlias www.my-domain-name.ch
DocumentRoot /var/www/node/prod/production
SSLProxyEngine on
ProxyPass /account/ https://www.account-server.com/account/ retry=1
ProxyPassReverse /account/ https://www.account-server.com/account/
ProxyPass /oauth2/ https://www.account-server.com/oauth2/ retry=1
ProxyPassReverse /oauth2/ https://www.account-server.com/oauth2/
ProxyPass / http://localhost:3001/ retry=1 Keepalive=on
ProxyPassReverse / http://localhost:3001/
CustomLog logs/access_log_production "%v %h %l %u %t \"%r\" %>s %b production"
<Directory /var/www/node/prod/production>
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName test.my-domain-name.ch
DocumentRoot /var/www/node/test/testing
SSLProxyEngine on
ProxyPass /account/ https://www.account-server.com/account/ retry=1
ProxyPassReverse /account/ https://www.account-server.com/account/
ProxyPass /oauth2/ https://www.account-server.com/oauth2/ retry=1
ProxyPassReverse /oauth2/ https://www.account-server.com/oauth2/
ProxyPass / http://localhost:3000/ retry=1 Keepalive=on
ProxyPassReverse / http://localhost:3000/
CustomLog logs/access_log_staging "%v %h %l %u %t \"%r\" %>s %b staging"
<Directory /var/www/node/test/testing>
AllowOverride All
</Directory>
</VirtualHost>
The error message that I am getting is:
[Mon Sep 07 11:47:35.784583 2020] [proxy_http:error] [pid 46205] [client 10.19.65.68:44942] AH01114: HTTP: failed to make connection to backend: localhost
[Mon Sep 07 11:47:46.049988 2020] [proxy:error] [pid 46206] (111)Connection refused: AH00957: HTTP: attempt to connect to 127.0.0.1:3000 (localhost) failed
Can anybod help me understand how to configure things where when one virtual host goes down, it doesn't bring down the others?
Many thanks in advance.
Nic3500 - thank you for your answer:
You cannot, as long as you have all the configuration in the same files, running under the same process. If you want to have a test setup, it should be under a separate Apache configuration. You can run two instances with different configurations on the same machine, as long as you do not use the same port for both. Or setup a VM into which you can put your test configuration (or docker, or ...). You could run apache -t to check your configuration before restarting it.

Why is my root directory accessible from apache?

I was playing around when I suddenly noticed that my browser could access my root directory just by typing "/" in the address. I checked the httpd.conf, it clearly states:
<Directory "/">
AllowOverride none
Require all denied
</Directory>
I am on Fedora 29 and this is my httpd.conf:
ServerRoot "/etc/httpd"
Listen 80
LimitRequestBody 5242880
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root#localhost
<Directory "/">
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
LimitRequestBody 5242880
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
LimitRequestBody 5242880
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
LimitRequestBody 5242880
AllowOverride None
Options +ExecCGI
Require all granted
SetHandler fcgid-script
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
LimitRequestBody 1024000
<Directory "/var/www/cgi-bin/data">
Order Deny,Allow
Deny From All
Allow From 127.0.0.1
</Directory>
Why is this happening?
My root directory was not accessed by apache. It was file uri parameters.

How to add a second site in httpd.conf

I'm using the following configuration of httpd.conf in my CentOs 7 Apache server to run "site1":
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root#localhost
ServerName locahost:80
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html/site1"
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
<Directory "/var/www/html/site1">
Options FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
When I access "http://localhost", site1 and its subpages/subfolders work correctly.
I want now to be able to display a second website: when accessing "http://localhost/site2", I want to display the content of a file "test.html" saved under "/test"; how should I edit the httpd.conf to make it work?
that's not a different site, it is a different directory. A different site would involve a different hostname.
Since you already have DocumentRoot "/var/www/html/site1 and you don't seem to want to create a new virtualhost you can point to a new/different directory with Alias
Also you just want site2 to load test.html under /test/ If I understood correctly when visited, just add the proper DirectoryIndex directive to it.
Here it is:
Alias /site2 /var/www/html/site2
<Directory /var/www/html/site2>
DirectoryIndex /test/test.html
</Directory>
For this to work you need these two modules:
mod_alias
mod_dir (but you may already have this one as you are already using DirectoryIndex directive)
You could also mkdir site2 under site1 but, this may look cleaner.

WAMP page not displaying over internet

I am running my WAMP server on my laptop running Windows 8.
I have a site set up at /www/MySite and made some Changes to my httpd.conf file as to make it accessable on the web, I've opened port 8080 in my router, and I've changed the ports to 8080 in my config.
Here's what I can do: I can get the site to work on these addresses:
http://127.0.0.1:8080/ok4pgm/
http://localhost:8080/ok4pgm/
http://192.168.1.77:8080/ok4pgm/
the first addresses can't be used for obvious reasons, and I'm guessing the second can't be used either since the address to my router begins with 192.168 and I therefore suspect it's some kind of local thing.
My IP address seems to be 213.114.161.6 (WhatsMyIP.org, canyouseeme.org & no-ip client) but it won't work on http://213.114.161.6:8080/ok4pgm/ - not even if I momentarily disable my firewall (and no, canyouseeme can't see port 80 nor 8080)
my httpd.conf:
ServerSignature On
ServerTokens Full
ServerRoot "c:/wamp/bin/apache/apache2.4.9"
Define APACHE24 Apache2.4
Listen 0.0.0.0:8080
Listen [::0]:8080
LoadModule php5_module "c:/wamp/bin/php/php5.5.12/php5apache2_4.dll"
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin admin#example.com
ServerName localhost:8080
HostnameLookups Off
DocumentRoot "c:/wamp/www/"
<Directory />
AllowOverride none
Require all granted
</Directory>
<Directory "c:/wamp/www/">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "c:/wamp/logs/apache_error.log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "c:/wamp/logs/access.log" common
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "c:/wamp/bin/apache/apache2.4.9/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "c:/wamp/bin/apache/apache2.4.9/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig conf/mime.types
AddEncoding x-compress .Z
AddEncoding x-gzip .gz .tgz
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php .php3
</IfModule>
EnableSendfile off
AcceptFilter http none
AcceptFilter https none
Include conf/extra/httpd-autoindex.conf
<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
Include "c:/wamp/alias/*"
please, please help.
Your router is a computer. When someone makes a request to your Internet facing IP address, they are making it to your router and not to your workstation.
As well as opening the port on your router, you also need to configure port forwarding so that the router will pass on the request to your workstation.