Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: Complicated requirements |
|
Author |
|
tkmilbaugh
Joined: 21 Aug 2025 Posts: 3 Location: USA
|
Posted: Thu 21 Aug '25 14:46 Post subject: Complicated requirements |
|
|
I have an existing, working httpd setup, running in a container, that uses a list of rewrite rules for redirecting obsolete URLs, and a reverse proxy setting to pass all other requests to another app, running in a second container in my Kubernetes pod.
What I want to add to the setup is a list of partial URLs that will serve static content from an attached shared drive. If a request like one of these comes along:
https://my-host.com/this/*
https://my-host.com/that/*
then I will map the request to a static file in:
/path/to/my/content/this
/path/to/my/content/that
Any request not matching any of these patterns will continue to be proxied to my application.
I have read a lot of Apache documentation, but I have not quite figured out how to do this. Is it possible?
TIA |
|
Back to top |
|
tangent Moderator
Joined: 16 Aug 2020 Posts: 396 Location: UK
|
Posted: Fri 22 Aug '25 15:10 Post subject: |
|
|
You should be able to achieve this requirement using suitable AliasMatch directives, followed by selective proxy requests, e.g.:
Code: | # Alias requests for /this/* and /that/* to local shared content
#
AliasMatch "^/this/(.*)$" "/path/to/my/content/this/$1"
AliasMatch "^/that/(.*)$" "/path/to/my/content/that/$1"
|
If the path to your local shared content is outside of the Apache DocumentRoot, you'll need to grant anonymous access to it, e.g.:
Code: | # Grant access to shared content
#
<DirectoryMatch "/path/to/my/content/(this|that)/">
Require all granted
</DirectoryMatch>
|
You can then proxy requests for all other content within a suitable LocationMatch directive (note the following uses a negative lookahead regex), e.g.:
Code: | # Proxy requests other than /this/* /that/*
#
<LocationMatch ^/(?!this|that)(.*)$>
ProxyPass http://my-proxy-host.com/$1
ProxyPassReverse http://my-proxy-host.com/$1
</LocationMatch>
|
|
|
Back to top |
|
tkmilbaugh
Joined: 21 Aug 2025 Posts: 3 Location: USA
|
Posted: Tue 26 Aug '25 15:00 Post subject: |
|
|
I was able to get the "this" or "that" part working with this configuration:
Code: | Alias /this/ /path/to/this/
<Directory /path/to/this/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /that/ /path/to/that/
<Directory /path/to/that/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
|
However, the code that handles redirects no longer works with the existing configuration:
Code: | <VirtualHost *:80>
RewriteEngine On
UseCanonicalName On
ServerName my.host.net
Include /path/to/rewrite/rules/rules.config
ProxyPass / http://localhost:8080/
ProxyPassReverse / /http://localhost:8080/
</VirtualHost>
|
If I comment out the ProxyPass and ProxyPassReverse lines, then "this" or "that" works, but the external application is unreachable. If I leave the reverse proxy in place, then my local static files are not found, but the external application works.
If I include the rewrite rules outside of the virtual host configuration, then none of the redirects will function.
Also, adding the rewrite rules to the suggested reverse proxy code didn't work either (although the reverse proxy is working):
Code: | <LocationMatch ^/(?!this|that)(.*)$>
ProxyPass http://localhost:8080/$1
ProxyPassReverse http://localhost:8080/$1
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
Include /path/to/rewrite/rules/rules.config
</LocationMatch>
|
|
|
Back to top |
|
tangent Moderator
Joined: 16 Aug 2020 Posts: 396 Location: UK
|
Posted: Tue 26 Aug '25 21:45 Post subject: |
|
|
Your original post didn't mention virtual hosts, nor detail the salient points of your configuration layout, or sample rewrite rules.
Nonetheless, of choice I would personally aim to define URL rewrite logic, as well as your "this" or "that" logic, at the global level. However, I note you say:
tkmilbaugh wrote: | If I include the rewrite rules outside of the virtual host configuration, then none of the redirects will function.
|
With your rewrite logic at the global level, you'll need to inherit those rules within each virtual host, with a suitable RewriteOptions setting, so any rewrite changes to the request should be honoured when entering the virtual host, e.g.
Code: | <VirtualHost *:80>
# Inherit mod_rewrite rules
#
RewriteEngine On
RewriteOptions InheritBefore
...
...
</VirtualHost> |
Equally, I'm assuming you've nested the <LocationMatch> block withing your <VirtualHost> block?
This layout construct for supporting proxy connections has worked for me in the past. |
|
Back to top |
|
tkmilbaugh
Joined: 21 Aug 2025 Posts: 3 Location: USA
|
Posted: Fri 29 Aug '25 17:20 Post subject: |
|
|
tangent wrote: | Your original post didn't mention virtual hosts, nor detail the salient points of your configuration layout, or sample rewrite rules.
|
Indeed, and I apologize for not including all of the detail 😕
I greatly appreciate your assistance! This configuration seems to be working.
Code: | Alias /this/ /path/to/this/
<Directory /path/to/this/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /that/ /path/to/that/
<Directory /path/to/that/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Include /path/to/rewrite/rules/rules.config
<VirtualHost *:80>
UseCanonicalName On
ServerName my.host.net
RewriteEngine On
RewriteOptions InheritBefore
<LocationMatch ^/(?!this|that)(.*)$>
ProxyPass http://localhost:8080/$1
ProxyPassReverse http://localhost:8080/$1
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</LocationMatch>
</VirtualHost>
|
My rewrite rules are all generally in this form:
Code: | RewriteRule ^/foo$ /some/other/path/bar [R=301,L,NC] |
Thanks tangent! |
|
Back to top |
|
|
|
|
|
|