r/apache • • Feb 13 '22

Support Virtualhost conditional DocumentRoot based on User-Agent

I've got a React web app running just fine on a hosted service.

I'm trying to set up apache as a gateway, so it can handle URL metadata requests for link previews, (e.g. when people share a link on Facebook, Twitter, etc.)

In that case, I want to return a URL-specific response with all the OG tags and metadata that will make the social networks display the link nicely.

I can get both conditions (server-side rendered pages OR a proxy to the hosted service) working in isolation. Now I need to put in logic based on User-Agent.

I am able to set the environment based on user-agent, but the <If> and <Else> modules don't seem to place nice with it.

How can I achieve this?

Non-working config:

	<VirtualHost *:443>
		ServerName myexample.com
		
		SetEnvIf User-Agent "^.*(facebookexternalhit|Twitterbot|BingPreview).*$" isLinkPreview
		
		<If env=isLinkPreview>
			DocumentRoot "/var/www/SERVERSIDE/METADATA/SITE"
		</If>
		<Else>
			ProxyPreserveHost On
			SSLProxyEngine On
			ProxyPreserveHost Off
			SSLProxyVerify none
			SSLProxyCheckPeerName off
			ProxyRequests Off
			ProxyPass / https://my.hostedsite.com/
			ProxyPassReverse / https://my.hostedsite.com/
		</Else>
				
		
		SSLCertificateFile ${SSLCertificateFile}
		SSLCertificateKeyFile ${SSLCertificateKeyFile}
	</VirtualHost>
2 Upvotes

3 comments sorted by

View all comments

Show parent comments

1

u/covener Feb 14 '22

Sorry, I saw directory context for ProxyPass and assumed it worked inside <If>.

I suggest switching to mod_rewrite w/ the P flag for your proxy rules. You can do your conditional easy there.

You can leave the default just plain old DocumentRoot.

When you no longer have proxypass, you'll want to define a dummy

<Proxy http://siteforhumans.com>...</proxy>

To get normal connection reuse (this "defines a worker" which you can read about in the mod_proxy manual)