apache 2.4 - deny direct ip access and show custom error page - apache

I want requests to http://example.com to serve my website, and requests to http://123.123.123.123 to show a customized 403 error page.
I have Apache/2.4.29 running on my Ubuntu server.
/etc/apache2/sites-available/vhosts.conf
<VirtualHost *:80>
ServerName default
<Location />
Require all denied
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options None
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
/etc/apache2/conf-available/security.conf
Alias /errors/ "/var/www/html/error-pages/"
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
<Directory /var/www/html/error-pages>
Require all granted
</Directory>
With the above setup direct-ip access is blocked, however it is not showing my custom error page.
Forbidden You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to
use an ErrorDocument to handle the request.
Then I tried using the directive instead of :
<VirtualHost *:80>
ServerName default
<Directory />
Require all denied
</Directory>
</VirtualHost>
Now all subdirectories (e.g. http://123.123.123.123/sub) show my error page, but unfortunately the root is not blocked at all and showing my index.php page.
My next guess was to experiment with wilcards:
<VirtualHost *:80>
ServerName default
<Location "/*">
Require all denied
</Location>
<Location "/*/">
Require all denied
</Location>
</VirtualHost>
This code works, and shows my own 403 error page on the root aswell all the subdirectories.
All this got me confused!
Is the how it should be done, or am I doing something wrong in the first two examples?

<VirtualHost _default_:80>
<Directory "${SRVROOT}/htdocs">
Require all denied
</Directory>
ErrorDocument 403 "Sorry, direct IP access not allowed."
ErrorLog "${SRVROOT}/logs/default-http-error.log"
CustomLog "${SRVROOT}/logs/default-http-access.log" combined
</VirtualHost>
This is what I have setup in my server where the directory directive is to denied access to all files in this virtualhost. And the errordocument 403 serving as a message. Other syntax are for error logging. Make sure this virtual host is before any other virtualhost. Something similar for the HTTPS scheme if you support HTTPS on your server.

Related

Proxy Apache FastCGI

I have a domain with a python django application and the request tracker software.
The goal of the post is to move the request tracker to the path "^/ ticket /*"
That's why the consideration is "mod_proxy_fcgi" (or does one rather use "mod_proxy_http"?).
Original working 000-default.conf code without proxy:
<VirtualHost *:80>
DocumentRoot /opt/rt4/share/html
Alias /NoAuth/images/ /opt/rt4/share/html/NoAuth/images/
ScriptAlias / /opt/rt4/sbin/rt-server.fcgi/
<Directory "/opt/rt4/sbin">
Require all granted
</Directory>
</VirtualHost>
Changed code with proxy settings (does not work):
<VirtualHost *:80>
ProxyPassMatch "^/ticket/*" "fcgi://localhost:80/opt/rt4/sbin/rt-server.fcgi/"
Alias /NoAuth/images/ /opt/rt4/share/html/NoAuth/images/
ScriptAlias / /opt/rt4/sbin/rt-server.fcgi/
<Directory "/opt/rt4/sbin">
Require all granted
</Directory>
</VirtualHost>
The proxy redirect returns HTTP code 503 "Service Unavailable" (http://localhost/ticket/).
Where am I wrong?

(Apache) Forbidden 403 when accessing subdomain

I want to set up the subdomain "shop" on my Apache 2.2 server on Windows/XAMPP and it redirects to "index.html" if I enter "shop.localhost" as the URL but once I try to connect via my domain "sv443.net" it redirects to my document root ("/index.html" instead of "/shop/index.html"). Connecting to "sv443.net/shop/" works fine though and even redirects to "index.html".
I'm using Cloudflare to handle the DNS stuff and added these records (I cut off the IP address part):
This is my httpd-vhosts.conf:
NameVirtualHost localhost:80
<VirtualHost localhost:80>
ServerName localhost
ServerAdmin sven.fehler#web.de
DocumentRoot "c:/users/sv443/desktop/mamp htdocs"
<Directory "c:/users/sv443/desktop/mamp htdocs">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost shop.localhost:80>
ServerName shop.localhost
ServerAdmin sven.fehler#web.de
DocumentRoot "c:/users/sv443/desktop/mamp htdocs/shop"
<Directory "c:/users/sv443/desktop/mamp htdocs/shop">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
And this is my hosts.dat:
(only comments)
2.205.169.73 sv443.net
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost shop.localhost
Also I added this .htaccess file to the shop directory to redirect to the index.html if a 403 or 404 is encountered - removing this file doesn't fix my problem:
ErrorDocument 403 /shop/index.html
ErrorDocument 404 /shop/index.html
If someone tries to connect I get this error message in the Apache error.log:
AH01630: client denied by server configuration: C:/Users/Sv443/Desktop/MAMP htdocs/shop/.html
Thanks for any help!
If you can get content by explicitly specifying "/index.html" but not with "/", you should add DirectoryIndex index.html in your configuration, after loading mod_dir. This will define the default file to look for when it is not specified.
For your VirtualHost:
<VirtualHost *:80>
ServerName localhost
ServerAlias www.example.com
ServerAlias example.com
LogLevel debug
CustomLog "logs/example_access_log" combined
ErrorLog "logs/example_error_log"
[... REST OF CONFIGURATION ...]
</VirtualHost>
<VirtualHost *:80>
ServerName shop.localhost
ServerAlias shop.example.com
LogLevel debug
CustomLog "logs/shop_access_log" combined
ErrorLog "logs/shop_error_log"
[... REST OF CONFIGURATION ...]
</VirtualHost>
Apache decides which VirtualHost to use for the request based on the domain in the request. If it does not find a match, it uses the first one in the file (here localhost). Therefore you are not pointing to your "shop" sub-domain, unless you specify it in the VirtualHost for it, via ServerAlias ....

Default index.html page for any virtual host

I have configured some vhost like:
<VirtualHost *:443>
ServerName test.mywebsite.com
ServerAlias test.mywebsite.com
DocumentRoot /hosting/test
<Directory "/hosting/test">
AllowOverride All
Order Allow,Deny
Allow from All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /usr/local/apache/ssl/mywebsite.com.pem
SSLCertificateKeyFile /usr/local/apache/ssl/mywebsite.com.key
</VirtualHost>
Now i need to display a default webpage if domain root folder not have any index page like index.php or index.html.
Actualy if open wesite it return 403 forbidden if index.html/php is not present.
My idea is;
Folder site no index.html/php show:
defaultwebpage.html in /etc/var/http/hdocs/default.html
How to this?
Apache config.conf is configured with default root but if put the index.html here it is not displayed in case of lack.
DocumentRoot "/data/www/default"
<Directory "/data/www/default">
Couldn't you use the ErrorDocument thing to redirect all 404 to a certain default page? If that default page is outside your webroot use alias to bring it in.
So
Alias /specialindex /etc/var/http/hdocs/
and
ErrorDocument 403 /specialindex/default.html
Looks strange, but should work. But, catches all 403 of course...
The solution:
<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>
<Directory /usr/share/httpd/noindex>
AllowOverride None
Require all granted
</Directory>
Alias /.noindex.html /usr/share/httpd/noindex/index.html

httpd subdomain 404 error

I have a working site at on the Top level of a domain, I now want to add a subdomain on the same domain and the same server.
The files for this site are located in the public html folder (/var/www/html)
I have the following virtualhosts/directory config in httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin support.portal#somesite.com
DocumentRoot /var/www/html/joomla3
ServerName support.somesite.com
ErrorLog logs/support.somesite.com_error.log
CustomLog logs/support.somesite.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin support.portal#somesite.com
DocumentRoot /var/www/html/moodle
ServerName support.somesite.com/moodle
ErrorLog logs/moodle.error.log
CustomLog logs/moodle.access.log common
</VirtualHost>
<Directory "/var/www/html/moodle">
DirectoryIndex index.php
AcceptPathInfo on
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
No errors are reported on the start/restart of httpd
Nothing is reported in the httpd error log. The below entry is shown in the Access log so the request is reaching the webserver but appears to be not being handled properly by the webserver.
192.168.4.202 - - [05/Jan/2015:12:49:14 +0000] "GET /moodle HTTP/1.1" 404 290
I have configured something similar before but I can't figure out the problem here. I might have missed something obvious, Can you see where the problem is, Thanks?
I am running CentOS 6.5 for reference.

What is wrong with this apache virtualhost directive?

I'm getting 403 forbidden errors with this virtualhost directory in apache 2.2. Can anyone help, please?
<VirtualHost *:80>
ServerName www.xyz.com
ServerAlias xyz.com
DocumentRoot "/home/bruce/projects/links/www"
<Directory />
AllowOverride None
Options Indexes FollowSymLinks
Order allow,deny
Allow from all
</Directory>
DirectoryIndex index.html index.htm index.jsp index.php
</VirtualHost>
Thanks!
EDIT error in log is
(13)Permission denied: access to / denied
If i'm not wrong you have indicated the root directory of you file system in < Directory>
It should be:
<VirtualHost *:80>
ServerName www.xyz.com
ServerAlias xyz.com
DocumentRoot "/home/bruce/projects/links/www"
<Directory /home/bruce/projects/links/www>
...
Gah. It ended up being that I was indeed forbidden from accessing that page. I am using a new virtual server provider, and they have set up my /home account as readable only by me. As a result, the user running apache was unable to view the files. I've changed the permissions on my home directory to be more lenient and now everything works as expected.