CloudFlare and Apache Virtual Hosts

Hi,

I have a VPS running lamp with virtual hosts enabled. Virtual hosts works great except for one domain for which it’s A record goes through cloudflare. When trying to access that domain the apache server returns the default configuration instead of the specific domain configuration.

From looking at apache logs it seems to me that the request going through cloudflare does not provide the server with the domain name requested. (basically it doesn’t say to the server show me xyz.com domain - it just says show me the web page and the server returns the default configuration)

When the cloudflare proxy service is disabled on the A records in the DNS section the website works as expected without any issues.

What would be a correct configuration for using cloudflare with multiple virtual hosts on apache 2.4?

Thanks

No reason that shouldn’t work, works for me (and presumably thousands of people who host on things cloud platforms that also reply on a host being passed in the header). Can you share your Apache config?

Thanks for the reply Saul.

Sure, here is my config…nothing special…without cloudflare works great, it’s very strange.

My apache2.conf:

# Global configuration
#
Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 300

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /sh/>
    AllowOverride None
    Require all granted
</Directory>

<Directory /www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" $
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combin$
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

ServerName sub.domainname.com

My virtual host file for the specific domain:

<VirtualHost domainname.com:80>
    # The ServerName directive sets the request scheme, hostname and port t$
    # 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 domainname.com
    ServerAlias www.domainname.com
    ServerAdmin [email protected]
    DocumentRoot /www/public_html/domainname

    <Directory "/www/public_html/domainname">
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            php_admin_value open_basedir "/www/public_html/domainname/:/temp"
    </Directory>

    # 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 ${APACHE_LOG_DIR}/domainname-error.log
    CustomLog ${APACHE_LOG_DIR}/domainname-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

</VirtualHost>


# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Let me know if you notice something that shouldn’t be there. This issue has been stopping me from using cloudflare for quite some time now.

Thank you

I also tried removing the domain name in the virtual host configuration starting tag: <VirtualHost *:80> and nothing changes.

Is it really HTTP (:80) only?

Hi Mark,

Yes, no ssl yet.

Any ideas anybody?

Is there a way to see what cloudflare requests look like? A debug mode or anything like that?

Hi Sandro,

Thank you for replying. Fortunately I managed to figure it out myself. Cloudflare sends the correct host and the issue sits in Apache.

Here is the solution for other users having the same issue:

Turns out that Apache needs to be able to resolve the hostname as set in the virtual host file to its own IP. Since Cloudflare hides the server IP when using the proxy service (the orange cloud), Apache cannot resolve the hostname to itself and for some reason doesn’t use the configuration file for that specific domain thus delivering the default virtual host file. To fix this you need to add the domain that is using Cloudflare to the hosts file of the server, pointing it at itself.

I hope that makes sense. :slight_smile:

Thank you to everyone who replied and I hope this will help anybody facing the same issue.

This topic was automatically closed after 14 days. New replies are no longer allowed.