0

Running Apache 2.4.58

I need to get Apache to serve example.com from a directory and all subdomains *.example.com to be ProxyPass / http://localhost:8050 (so they can be served from another web server dependent on the subdomain).

I can currently get it to ProxyPass absolutely everything to the other localhost server or serve the directory for example.com then all subdomains are not ProxyPassed so fail to serve as they never hit the other server.

I've tried doing multuple virutalhosts but I can't seem to get it to work.

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyRequests Off
    
    ServerName example.com

    DocumentRoot /home/example/public_html
    ErrorLog /var/log/apcache2/example_error_log
    CustomLog /var/log/apache2/example_access_log combined
    DirectoryIndex index.php index.php4 index.php5 index.htm index.html

    <Directory /home/example/public_html>
        Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch 
        Require all granted
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
    </Directory>

    ServerAlias *.example.com
    ProxyPass / http://localhost:8050/
    ProxyPassReverse / http://localhost:8050/

</VirtualHost>

And a matching 443 version is under this.

Everything is passed to the Proxy when running the above. I thought it would do the first directives then the next. I have also tried with the last 3 lines at the top which made no difference.

Someone people help! It's driving me crazy. TIA.

3
  • 1
    Hi, how many virtualhost have you setup? you will need a virtual host for example.com and another virtualhost with wildcard for *.example.com and make a proxy pass in this one. Show us your config.
    – Skamasle
    Nov 12 at 18:10
  • I've tried doing multuple virutalhosts but I can't seem to get it to work.
    – Tom Kaiser
    Nov 12 at 18:16
  • Thanks, I've added the config in my question. There is just 1 Virtualhost file with this in the config and a matching 443 version underneath it.
    – Tom Kaiser
    Nov 12 at 18:28

1 Answer 1

1

You need two virtualhost for this, serveralias just work as alias of servername, so you cant define in that way, you can read documentation here: https://httpd.apache.org/docs/2.4/vhosts/name-based.html

Try this: 1 Virtual host for your main domain:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyRequests Off
    
    ServerName example.com

    DocumentRoot /home/example/public_html
    ErrorLog /var/log/apcache2/example_error_log
    CustomLog /var/log/apache2/example_access_log combined
    DirectoryIndex index.php index.php4 index.php5 index.htm index.html

    <Directory /home/example/public_html>
        Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch 
        Require all granted
        AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
    </Directory>


</VirtualHost>

And another one for your wildcard subdomains:

<VirtualHost *:80>
        ServerName *.example.com
        ProxyPass / http://localhost:8050/
 </VirtualHost>

Remember load proxy module in your apache configuration

LoadModule proxy_module modules/mod_proxy.so
1
  • Is a wildcard *.example.com valid for a ServerName directive? Doesn't that require a valid hostname? I usually only see the wildcard in combination with a ServerAlias
    – HBruijn
    Nov 14 at 15:22

You must log in to answer this question.

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