3

I need to return an error on wrong http_host header. For nginx I would write the following config:

server { 
    ## Deny illegal Host headers
    if ($host !~* ^(mydomain.com|www.mydomain.com)$ ) {
        return 444;
    }

    location  / {
        proxy_pass               http://app_server;
        ...
    }
}

How can I do the same for Apache?

UPDATE

Although the trick with virtualhosts does work, I ended up with RewriteRule:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ - [L,R=400]
0

2 Answers 2

4

here is an example

<VirtualHost _default_:80>
    ErrorDocument 403 /403.html
    ErrorDocument 404 /404.html
</VirtualHost>

just don't put ServerName or ServerAlias inside of this VirtualHost.

3
  • Thanks a lot, you saved hours of my time. I really have no time to read Apache docs from scratch right now.
    – Vlad T.
    May 8, 2014 at 20:42
  • @VladT. you're welcome) i think some things are easier in apache vs nginx.
    – alexus
    May 8, 2014 at 20:44
  • 1
    This doesn't always fully address the issue. It is better to add DocumentRoot /var/www/catchall and <Directory /var/www/catchall> Order deny,allow Deny from all </Directory>
    – ColinM
    Mar 13, 2015 at 21:25
-1

Just make a VirtualHost that matches whatever host header you're interested in and then return whatever error you want.

5
  • Don't you mean VirtualHost? The NameVirtualHost defines IP/Port on which name based vhosts should be used. I'd say return the error in the default vhost and only care for vhosts that actually exists :) But maybe I understand your intention wrong? May 8, 2014 at 19:56
  • 1
    @EEAA Could you please elaborate on your suggestion with simple example? I'm not familiar with Apache config syntax.
    – Vlad T.
    May 8, 2014 at 19:57
  • @FrandsHansen you're absolutely right. Corrected. And I agree, just send the error from the default vhost and be done with it.
    – EEAA
    May 8, 2014 at 19:57
  • @VladT. We don't spoonfeed answers here. Go do a bit of research, this is all very simple stuff. If you can configure nginx, you can surely configure apache.
    – EEAA
    May 8, 2014 at 19:58
  • 2
    @vladT. See here: httpd.apache.org/docs/2.2/vhosts Apache docs are very very good and easy to read/understand :) May 8, 2014 at 19:59

Not the answer you're looking for? Browse other questions tagged .