2

Our local development setup requires a box in the DMZ, and each developer has a line in its apache config for proxying. Looks something like:

ProxyPreserveHost on

ProxyPass /user1/ {user1's IP}
ProxyPassReverse /user1/ {user1's IP}

ProxyPass /user2/ {user2's IP}
ProxyPassReverse /user2/ {user2's IP}

#etc

Our public URLs become {DMZ server}/user1, {DMZ server}/user2, etc. The problem is that on the dev's boxes, the value of $_SERVER['HTTP_HOST'] is just {DMZ server}, without the user's subdirectory. The desired behavior is to have /user%/ as the real host name.

I've tried overriding the HOST var, and some rewrite rules, but nothing has worked.

Creating subdomains is not an option.

thank you for any help!

Taz
  • 3,718
  • 2
  • 37
  • 59
Ethan
  • 400
  • 3
  • 14
  • $_SERVER['HTTP_HOST'] only shows the hostname or IP address anyway. You need to use $_SERVER['REQUEST_URI'] or something similar to get the full path. – Corey Henderson Apr 18 '12 at 02:10
  • Believe me, I looked through all of $_SERVER for the right value. Besies, I'd rather not run through all projects that i run, and change code. PHP frameworks we use rely on HTTP_HOST as well. – Ethan Apr 18 '12 at 11:55
  • So if I understand the question correctly, the desire is to have {DMZ_SERVER}/user% passed through to the backend applications? – groodt Apr 24 '12 at 19:34
  • Unclear what you are trying to accomplish or even how you have things set up. What do you mean by "on the dev's boxes, the value of $_SERVER['HTTP_HOST'] is just {DMZ server}"? What is running on the dev boxes? Where do you need to see `{DMZ server}/user1`? You won't find `/user%/` as the host name ever, anywhere. You need to attack the problem a different way. – Old Pro Apr 24 '12 at 20:07
  • @OldPro: We have several proxy rules on that server in the DMZ, each one connects to a developer in the office. For us each to have a unique URL, the /%user path was set up. Running on the dev apps are apache and PHP mainly, for hosting various applications. So what is the different way? – Ethan Apr 25 '12 at 00:30

3 Answers3

3

http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypreservehost seems to be the answer.

Dmitry Ovsyanko
  • 1,416
  • 11
  • 6
1

Im going to take a stab and suggest this:

SetEnvIf Host (.*) custom_host=$1
RequestHeader set X-Custom-Host-Header "%{custom_host}e/%{REQUEST_URI}e/%{QUERY_STRING}e"

That should hopefully set a request header called X-Custom-Host-Header that you can then pickup in PHP. If you want, you can try to override the Host Header, but I'm not sure on the implications of that. The Host header is a special HTTP header and generally only contains the host portion of an HTTP request, not the full request url.

Untested unfortunately, but it would help if you could clarify in a bit more detail what you are looking for.

groodt
  • 1,955
  • 15
  • 26
  • Unfortunately i can't go through all the code replacing instances of 'HTTP_HOST', 'SERVER_NAME', 'SCRIPT_URI', etc. – Ethan Apr 25 '12 at 13:02
0

EDIT, THIRD ANSWER:

Looks like Apache has heard this complaint before and the solution is mod_substitute. You need to use it to rewrite all the URLs returned in the document to insert /user1/.

EDIT, SECOND ANSWER:

Based on the additional information in your comments, I'd say your Apache config on your DMZ server is correct. What you are asking for is to have the developer machines generate URLs that include their context path (which is the J2EE term for something analogous to your /user1/ bit). I don't have any experience with PHP so I don't know if it has such a facility, but a quick search suggests it does not.

Otherwise, you'd have to roll your own function that converts a relative URL to an absolute URL, make that configurable so you can have it add something to the host name, and then force everyone to use that function exclusively for generating URLs. See, for some guidance, "Making your application location independent" in this old (outdated?) PHP best practices article for a solution to the related problem of finding local files.

PREVIOUS ANSWER: (doesn't work, causes redirect loop)

I'm still not clear what you are trying to do or what you mean by "Running on the dev apps are apache and PHP mainly, for hosting various applications", but as an educated guess, have you tried:

ProxyPass /user1/ {user1's IP}/user1/
ProxyPassReverse /user1/ {user1's IP}/user1/

If I were setting up the sort of environment you seem to be wanting to have, I'd want $_SERVER['HTTP_HOST'] to be {DMZ server} on every dev machine so that the dev machine's environment looks just like (or at least more like) production to the code running on it.

Community
  • 1
  • 1
Old Pro
  • 24,624
  • 7
  • 58
  • 106
  • That results in a redirect loop for me, actually. – Ethan Apr 25 '12 at 13:07
  • The goal is to have a unique, public URL for each developer, which correctly forwards to their machine. Thats really it. When PHP needs to construct a URL (for an image, for example), it uses $_SERVER['SERVER_NAME'] or 'HTTP_HOST', which is incorrect in this case. Subdomains would be perfect, i think. – Ethan Apr 25 '12 at 13:09
  • @Ethan, subdomains would be ideal. What you are doing is easy in J2EE because it has the concept of the application context, and you construct urls not relative to the host name but relative to the context, which in your case would be host name plus `/user1/`. I don't know how PHP servers and apps are set up. In principle I'd write a function that converts a relative URL to an absolute URL, make that configurable so you can have it add something to the host name, and then force everyone to use that function exclusively for generating URLs. – Old Pro Apr 25 '12 at 17:25