Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto serve a second subdomain through Apache Proxy

I have an Apache frontend server, that until today proxies traffic for a subdomain api.myapp.net to a backend server running a Rails app on an Nginx.

Now I added a second subdomain alpha.myapp.net in my domain portfolio and gave it the same IP. Traffic to that subdomain shall not hit the Rails application, but a second VHost on the Nginx server that is setup to serve a static site.

So I have a proxy config for api.myapp.net:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName api.myapp.net
    ServerAlias api.myapp.net

    DirectoryIndex index.html

    RewriteEngine On

    RewriteLog /var/log/apache2/rewrite.log
    RewriteLogLevel 9

    RewriteCond %{HTTP_HOST} !^(api\.)?myapp\.net$
    RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301]

    ProxyPass / http://192.168.1.145/
    ProxyPassReverse / http://192.168.1.145/

    ErrorLog /var/log/apache2/error.log

    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature Off
</VirtualHost>

And I setup a second config for alpha.myapp.net:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName alpha.myapp.net
    ServerAlias alpha.myapp.net

    DirectoryIndex index.html

    RewriteEngine On

    RewriteLog /var/log/apache2/rewrite.log
    RewriteLogLevel 9

    RewriteCond %{HTTP_HOST} !^(alpha\.)?myapp\.net$
    RewriteRule ^(.*)$ http://myapp.net$1 [L,R=301]

    ProxyPass / http://192.168.1.145/
    ProxyPassReverse / http://192.168.1.145/

    ErrorLog /var/log/apache2/error.log

    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature Off
</VirtualHost>

What happens now, is, that all traffic to alpha.myapp.net hits my Rails application that listens for requests for api.myapp.net.

I sorted out all Nginx config problems, so I thought it must be an Apache misconfiguration. What I see in Apache's rewrite log seems to explain the problem:

xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (2) init rewrite engine with requested uri /index.html
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (3) applying pattern '^(.*)$' to uri '/index.html'
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (4) RewriteCond: input='alpha.myapp.net' pattern='!^(alpha\.)?myapp\.net$' => not-matched
xxx.yyy.zzz.95 - - [08/Apr/2013:09:34:35 +0200] [alpha.myapp.net/sid#b9279870][rid#b9311d38/initial] (1) pass through /index.html

The last part pass through /index.html seems to omit the subdomain and domain part. So the Nginx backend server does not now, which subdomain is requested, ans serves the request from the first available server, which ist api.

The question now seems to be: How can I proxy the traffic from Apache frontend to Nginx backend and maintain the subdomain and domain?

Or could there be another problem?

Hope someone can help.

Regards Felix

like image 426
GeorgieF Avatar asked Dec 11 '25 20:12

GeorgieF


1 Answers

Sometimes the answer comes along with the question. The problem was exactly the missing hostname.

I solved the problem by editing /etc/hosts on my Apache server and adding two entries. One api.myapp.net, one alpha.myapp.net both referencing the same IP.

Then I changed bot Apache proxy configurations, so that ProxyPass and ProxyPassReverse do not use IP's anymore but the new hostnames.

And voila it works.

like image 129
GeorgieF Avatar answered Dec 16 '25 23:12

GeorgieF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!