12

They say that Apache's mod_rewrite is the swiss-army knife of URL manipulation, but can it do this?

Lets say I want to add a new application to my Apache webserver, where the only configurable option of the app is a port number.

I want to use & give out URLs of the form "http://hostname.example.com/app" rather than "http://hostname.example.com:8080". This would ensure that clients would be getting through the institution's firewall as well, and it's generally tidier.

My application includes absolute URIs in php, javascript and css, so I want to prepend my own root location to the URI in the applications internal links. I have no access to DNS records and so can't create another name-based virtual server.

Using Apache's mod_rewrite and mod_proxy modules, I can transparently redirect a client to the correct home-page of the application. But links within that homepage don't point a client to links relative to the new base URL.

So, what's the best way of proxying a request to an application that is listening on a specific port?

For example, if I had an application listening on port 8080, I could put this in my Apache configuration:-

<VirtualHost *:80> SSLProxyEngine On ServerName myhost.example.com RewriteEngine On UseCanonicalName On ProxyVia On <Location "/application"> RewriteRule ^/application/?(.*) http://localhost:8080/$1 [P,L] </Location> </VirtualHost> 

This would work fine if the application didn't use absolute URLs, but it does. What I need to do is rewrite URLs that are returned by the application's css, javascript and php.

I've looked at the ProxyPass and ReverseProxyPass documentation, but I don't think these would work..?

I've also come across Nick Kew's mod_proxy_html, but this isn't included in the standard Apache Distribution, and my institution's webserver seems to have been fine for years without it.. Other than trawling manually (or using a grep -r | sed type expression) through the application's source code, or using this 3rd party add-on, are there any other ways to go about this?

Could I perhaps use some of the internal server variables in a mod_rewrite rule? For example a rewrite rule based on ’HTTP_REFERER'?

2
  • 2
    You need mod-proxy-html, because internal links in webpages might point outside application/, if they begin with / (they are not fixable by proxy, because e.g. /img/abc request is ambiguous without context: application/img/abc or really /img/abc). Cannot you fix the app to use only relative links? Commented Sep 24, 2011 at 12:16
  • Thought I replied to this... :S The apps in question are not directory based (daemon applications that listen on their own port) and don't refer to anything outside of their respective root directories. I did try fixing all of the absolute URIs in one app (pathway tools), but this just killed the app. This was also a lot of work, and would need to be repeated should an update replace anything important (some of which could be distributed as binary and thus would be uneditable; no doubt why this app broke). Ta tho :) Commented Sep 26, 2011 at 15:22

1 Answer 1

15

Using mod_proxy would work just fine. For instance, I mapped https://localhost/yalla/ to point to a subdirectory of my webserver:

LoadModule proxy_module modules/mod_proxy.so ProxyRequests On <Proxy *> Order deny,allow Allow from localhost </Proxy> ProxyPass /yalla/ http://yalla.ynfonatic.de/tmp/ 

If you implement this, you'll note that the pictues of the directory-listing aren't visible; this is because they're below the /tmp/ directory on the remote server, hence not visible.

So, in your case you'd do:

LoadModule proxy_module modules/mod_proxy.so ProxyRequests On <Proxy *> Order deny,allow Allow from localhost # Or whatever your network is if you need an ACL </Proxy> ProxyPass /app/ http://hostname.example.com:8080/ 

Like with everything in Apache configuration, watch those trailing slashes when referring to directories.

Good luck! Alex.

Sign up to request clarification or add additional context in comments.

4 Comments

Looks like this could work. Ta! Was looking at mod_proxy this morning actually, to see about auto-proxying my home network through the uni's one.. That's completely different functionality from this though. Will let you know how I get on. Cheers! Alex
This seems to drop preflight options requests
@nathan For that you might need some mod_rewrite sauce; I never stumbled upon that error, but a quick search revealed this link: songbard.wordpress.com/2011/05/27/… - not sure, if that helps, but feedback is welcome. YMMV.
ProxyRequests on seems a dangerous mistake: httpd.apache.org/docs/current/mod/mod_proxy.html#proxyrequests

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.