Solved

Proxy to multiple servers (getting /images to work)

Posted on 2002-07-05
25
581 Views
Last Modified: 2007-12-19
Hello!

I have a problem setting up a virtual host to proxy to several internal servers.

Example:
<VirtualHost www.mydomain.com>
    ServerName www.mydomain.com
    UseCanonicalName On
    <IfModule mod_proxy.c>
    ProxyVia On

    ProxyPass /server1 http://server_1.internal.mydomain.com/
    ProxyPassReverse /server1 http://server_1.internal.mydomain.com/

    ProxyPass /server2 http://server_2.internal.mydomain.com/
    ProxyPassReverse /server1 http://server_2.internal.mydomain.com/
    </IfModule>
</VirtualHost>

This works fine, but the problem is when these servers have an /images/ directory. Then all the images will not be displayed.
However if I only have one internal server and do ProxyPass / http://server_1.internal.mydomain.com it works fine.

Is there anyway to fix this (apart from changing something on the internal server) ?

TIA
/M
0
Comment
Question by:M031097
  • 13
  • 9
  • 2
  • +1
25 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7131540
hmm, did you miss the trailing / at the path?

ProxyPass /server1/ http://server_1.internal.mydomain.com/
ProxyPassReverse /server1/ http://server_1.internal.mydomain.com/
0
 

Author Comment

by:M031097
ID: 7131622
No, I am almost certain that I've tried that as well. (Unable to try it at the moment as I am at a different location)

Let me clarify:
On the internal servers there is an index.html file.
if the user goes to: www.mydomain.com/server1/ the proxy gets the index.html of server1.internal.mydomain.com.
However if there are links to images in index.html and these links start with i.e. /images/ it will not be displayed.
I have found that if I add:
ProxyPass /images http://server1.internal.mydoain.com
it works fine.
But how do I solve it for server2 in that case?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7131918
did you set in httpd.conf:
  options FollowSymLinks
0
 

Author Comment

by:M031097
ID: 7131966
No difference
0
 
LVL 15

Expert Comment

by:samri
ID: 7132011
M,

This might be a typo.  But notice the 2nd ProxyPassReverse.  Is it suppose to be /server2 ?

ProxyPassReverse /server1 http://server_1.internal.mydomain.com/

   ProxyPass /server2 http://server_2.internal.mydomain.com/
   ProxyPassReverse /server1 http://server_2.internal.mydomain.com/

Have you consider using mod_rewrite along with mod_proxy?  

How about using different VirtualHost and have ProxyPass/ProxyPassReserve in each to proxy the two different internal server.  This approach would work but instead of using www.domain.com/site1/ and www.domain.com/site2/, you might end up with site1.domain.com, and site2.domain.com with each would have;

<VirtualHost *>
ServerName site1.domain.com
ProxyPass / http://internal1.domain.com/
ProxyPassReverse / http://internal1.domain.com/
</VirtualHost>

<VirtualHost *>
ServerName site2.domain.com
ProxyPass / http://internal2.domain.com/
ProxyPassReverse / http://internal2.domain.com/
</VirtualHost>

I woulld believe rewriting would work, but would be a bit tricky.  Perhaps you might want to look at Rewriting Guide on Apache docs.  Try to jump to section that talks about "Dynamic Mirror",  and/or "Reverse Dynamic Mirror", and/or "Reverse Proxy".  It may/not be applicable, but worth to look at.

http://httpd.apache.org/docs/misc/rewriteguide.html

cheers;
0
 

Author Comment

by:M031097
ID: 7132061
Yes it was just a typo.
I have been looking at the rewrite module and are currently playing around with that.
Haven't got it to work though. The Dynamic Mirror sections seemed to be what I am looking for...well actually, I hope is what I'm looking for ;-)

0
 
LVL 15

Expert Comment

by:samri
ID: 7132089
M,

good luck.  

mod_rewrite is claimed to be quite powerful+flexible, if and only if one could get the rewrite condition+rule, otherwise it you would be spending long hours...

I am a bit surprise to why your initial config does not work.  It looks fine (except to the trailing slash as pointed by ahoffman).

Perhaps you might want to spend a few minutes on the Vhost approach.  I tried to do the same with EE, and microsoft (to so called accelerate it), and it works fine.  The catch is, it wont't work for hardcoded URL.

cheers.
0
 

Author Comment

by:M031097
ID: 7136995

Well I am a bit surprised it didn't work as well. I am a total newbie at this apache configuration thing, so I do not quite understand why it works when I do:
ProxyPass / http://server1.internal.mydomain.com/
and doesn't work when I do:
ProxyPass /server1/ http://server1.internal.mydomain.com/

Guess I'll have to work a bit more with the rewrite module and get that to work.

I am going to try the Vhosts.

Raising it to 300 points as well.
0
 
LVL 15

Expert Comment

by:samri
ID: 7137016
M,

The only possibility that I can think of on why

ProxyPass /server1 http://server_1.internal.mydomain.com/
ProxyPassReverse /server1 http://server_1.internal.mydomain.com/

does not work is that the link in HTML documents in the internal server (http://server_1) contains hardlink - ie <a href=http://server_1.internal.mydomain.com/somepage.html>A link</a>, compared to <a href=/somepage.html>A Link</a>.

Some would argue that mod_rewrite will be a bit slow (since some additional overhead is needed on the server side).  But it should work.

If you feels like looking at Rewriting, take a peek at;
http://httpd.apache.org/docs/misc/rewriteguide.html

Or jump to "Reverse Proxy" section.  There is a complete sample config that you could take a look.  Just make sure you had mod_rewrite.so loaded (in AddModules directive).

cheers.
0
 

Author Comment

by:M031097
ID: 7137099
Samri,

the main problem is when pages on server1 and server2 uses
/images/<imagename>.gif to display images, then all the users sees is broken links.
I'm having trouble understanding why at the moment
I guess it looks for images on www.mydomain.com under /images since if I add an proxypass for /images it works fine. But then, as I said, how do I know from which server (1 or 2) to fetch the image.

I'm reading the rewriting guide now...hope that helps me

/M
0
 
LVL 15

Expert Comment

by:samri
ID: 7137250
M,

Hmm.... that is a very interesting question.  And yes (to my knowledge) it would come from www.domain.com, but if the link is image/someting.gif (in HTML code), then it would be translated fine by ProxyPass and ProxyPassReverse.

Try the mod_rewrite, and see if that fixed the images portion.  Other than that. I would presume that using a Vhost for each accelerated server would solve the problem since there would be an assurance that /image for internal_server1 would always be coming from internal_server1.

cheers.
0
 

Author Comment

by:M031097
ID: 7137371
samri,

the vhosts doesn't quite work either, mainly because I want the users only to go to www.mydomain.com and then click on links.

1. User goes to www.mydomain.com
2. user clicks on the link server1
3. apache fetches server1.internal.mydomain.com
4. content of the server1 is displayed to the user
5. to the user it looks like he is at: www.domain.com/server1/

still working on rewrite though...trying to get that to work. The examples didn't work (or rather, I am new to it so I've probably missed something)

/M
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:M031097
ID: 7137575

hmm, been playing with the rewrite module while reading the URL Rewriting Guide. Now, the dynamic mirror part seems to be exactly what I want to do, but I am obviously missing something here.
I've changed my virtual host to this:
<VirtualHost *>
     UseCanonicalName On

     RewriteEngine  on
     <Directory / >
          RewriteBase     /news/
          RewriteRule     ^/(.*)$                    http://www.cnn.com/$1     [P]
          RewriteCond     /news/$1               -U
          RewriteRule      ^http://www\.cnn\.com/(.*)$     /news/$1
     </Directory>

</VirtualHost>

and if I understand it correctly, now when I go to the proxy and enter news/ I should see the content of cnn.com.
However, all I get is :
Not Found
The requested URL /news/ was not found on this server.

Any ideas?

Thanks in advance

/Markus

0
 
LVL 15

Expert Comment

by:samri
ID: 7138382
Markus,

I am not very good in rewriting myself.

Tried this example, and it worked.  well mine is running on win2k.


<Directory "C:/Program Files/Apache Group/Apache/htdocs">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
    RewriteEngine  on
    RewriteBase    /
    RewriteRule    ^stokely/(.*)$  http://www.stokely.com/$1  [P]
</Directory>

And the requested URL is: http://localhost:8000/stokely/index.html

try to stay away from Vhost at the moment (or maybe you might give it a shot anyway).

The reason why I picked Stokely is that I am pretty much comfortable with the fact that the site doesn't have much external link, which might render undisplayed link (or images).

Hope this gets you going.

cheers.
0
 

Author Comment

by:M031097
ID: 7138445

Samri,

I got it to almost work with the following:

RewriteCond %{HTTP_REFERER} .*/news/.*
RewriteRule ^/(.*) /news/$1
RewriteRule ^/news/(.*) http://www.cnn.com/$1
ProxyPassReverse /news/ http://www.cnn.com/

I am not at the place where I tried this so this is as I recall it.
It works pretty good, but I need to fine-tune it 'cause the HTTP_REFERER value changes at a point, that is the rewrite-rules don't work in some places. Have to debug it further tomorrow.

Will try what you did as well, to see how that works.

thanks

/M
0
 
LVL 15

Expert Comment

by:samri
ID: 7139520
Markus,

That is the beauty (or the danger) of apache rewriting engine.

Your example might work.

some people would discourage the use of HTTP_REFERER variable since it might not be availabe in some sessions.  

give it a shot.  I would be interested to know too..

regards.
0
 

Author Comment

by:M031097
ID: 7140540
Samri,

This is what I've come up with now, it works ok if, and it's a big if, the remote server html-pages are coded in a good way.

I noticed that the referer header got re-written if the link on the page did -not- start with an /
ex:
 <a href="index.html"
compared to:
 <a href="/index.html"
the latter works fine, the former doesn't (well the page will be displayed but links to /images/*.gif will not)

anyway, here is part of my httpd.conf:

<VirtualHost *>
    UseCanonicalName On

    RewriteEngine On
    RewriteLog logs/rewrite.log
    RewriteLogLevel 0
   
   
#Disable all caching
    NoCache *

    ProxyVia Block

# Rewrite/ProxyPassReverse for server www.cnn.com "located" in /_news/
    ProxyPass /_news/ http://www.cnn.com/
    RewriteCond  %{HTTP_REFERER} ^.*/_news/.* [NC]
    RewriteRule  ^/(.*)  /_news/$1 [C]
    RewriteRule  ^/_news/(.*) http://www.cnn.com/$1 [P]
    ProxyPassReverse /_news/ http://www.cnn.com/
</VirtualHost>


You are right regarding the HTTP_REFERER (as I mentioned above). What I would like is to somehow get the remote server address, or a way to save it in an enviroment variable. I looked at the SetEnv but couldn't quite figure out how to make it work. (I mean in conjunction with RewriteRule, SetEnv was simple enough ;-)

well...it feels like I've gotten about as far as I can on this. Unless you, or anyone else, has some brilliant way to make it fool proof :)

regards
/Markus
0
 

Author Comment

by:M031097
ID: 7143434
I noticed that I get: /_news/_news/ sometimes, so I added:
RewriteCond  %{REQUEST_URI} !^/_news/.* [NC]
after:
RewriteCond  %{HTTP_REFERER} ^.*/_news/.* [NC]


------------------------------------
ProxyPass /_news/ http://www.cnn.com/
RewriteCond  %{HTTP_REFERER} ^.*/_news/.* [NC]
RewriteCond  %{REQUEST_URI} !^/_news/.* [NC]
RewriteRule  ^/(.*)  /_news/$1 [C]
RewriteRule  ^/_news/(.*) http://www.cnn.com/$1 [P]
ProxyPassReverse /_news/ http://www.cnn.com/


/M
 
0
 
LVL 15

Expert Comment

by:samri
ID: 7143720
Markus,

There goes the *newbie* in mod_rewrite.  To be honest, you far exceed my expectation for somebody new to apache. (gee... looks like I am learing more than what I initially tend to...  umm... help :)

anyway,I will be scanning for anything that I could jump in and give a hand.  At the moment, I'm flipping the mod_rewrite manual (and kinda learnign from you.).

There is another Q that is closely similar to yours which I happens to be *learning* too.
http://www.experts-exchange.com/questions/Q_20319552.html

cheers.
0
 

Author Comment

by:M031097
ID: 7143783

Well
I have solved my problem (right now actually :)
.....funny, when you're stuck in a line of thought...it's hard to break it ;-)

I happend to see an example on virtual hosts and that helped me. (I had made some error when I tried it previously, which is the reason why I didn't think it would work)

anyway, this is what I did:

NameVirtualHost IP-number
<VirtualHost IP-number>
    ServerName proxy.mydomain.com
</VirtualHost>

<VirtualHost IP-number>
    ServerName bogus-name1.proxy.mydomain.com
    ProxyRequests on    
    ProxyVia On
   
#Disable all caching
    NoCache *

   ProxyPass / http://back-endserver1.mydomain.com/
   ProxyPassReverse / http://back-endserver1.mydomain.com/
</VirtualHost>

<VirtualHost IP-number>
    ServerName bogus-name2.proxy.mydomain.com
    ProxyRequests on    
    ProxyVia On
   
#Disable all caching
    NoCache *

   ProxyPass / http://back-endserver2.mydomain.com/
   ProxyPassReverse / http://back-endserver2.mydomain.com/
</VirtualHost>

I also added these bogus names in the hosts file
and then just edited the .html-file to
<a href="http://bogus-name2.proxy.mydomain.com/">News</a><br>

works!

I learned alot from this though, just hope I can remember all this Rewriting stuff...pretty cool :)

Anyway, thanks for the input
and hope this helps the other guy to...
0
 
LVL 15

Expert Comment

by:samri
ID: 7143916
markus,

It's great to hear that it finally gets somewhere..

sure hope that Lars catches this one too.

Since you technically solved you own problem; at this stage; you are left with two options (do I sound like a State Trooper :), you can post a quesstion in Community Support (http://www.experts-exchange.com/commspt/) to request for a deletion to the Q (which I would object), or get your pts refunded, by getting the Moderator to reduce the pts to 0, and that way the invaluable information is kept as PAQ, or (here comes the best part), you could award the member who you feels like awarding.  You can split pts too, ask the Moderators.

gee.. checked you profile -- an EE veteran! just ignore my comment :)

cheers.

0
 

Author Comment

by:M031097
ID: 7144525
hahaha, a veteran? yes maybe, but only date wise ;-)
anyway, thanks again, I'll contact a Moderator and PAQ it.

cheers
/Markus
0
 
LVL 6

Accepted Solution

by:
Mindphaser earned 300 total points
ID: 7144543
Points refunded and moved to PAQ, akser provided solution.

** Mindphaser - Community Support Moderator **
0
 

Author Comment

by:M031097
ID: 7145311
samri
I'm going to award you some points since your input was very valuable. See topic area.
0
 
LVL 15

Expert Comment

by:samri
ID: 7145487
Markus,

Yep... I got it (http://www.experts-exchange.com/apache/Q_20322005.html)

I just wished that I could be of more help.

Anyway, many thanks for the pts.


cheers.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now