Another problem with slashes

New to mod_rewrite? This is a good place to start.

Another problem with slashes

Postby huggie » Fri Jul 10, 2009 8:48 am

I have multiple apache instances running on the same server. The main instance runs on port 80 and acts as a proxy forwarding requests to the other instances that are running on ports 8001 .. 8009 using mod_rewrite.

The reason for this setup is so that each instance can be accessed from the browser without the need to remember port numbers, and without the port numbers being displayed in the address bar.

The instances all have a DNS entry pointing to the server IP e.g

192.168.0.1 proxy (Running on port 80)
192.168.0.1 server1 (Running on port 8001)
192.168.0.1 server2 (Running on port 8002)

The setup is working almost flawlessly, but I have one problem with trailing forward slashes.

If I enter http://server1 in the address bar I get forwarded on to 192.168.0.1:8001. This is correct, the address bar is also correct just showing http://server1

I have a sub directory under server1 called 'something', If I enter http://server1/something/ (note the forward slash) in the address bar I get forwarded on to 192.168.0.1:8001/something. This is correct, the address bar is also correct just showing http://server1/something/

But if drop the forward slash, just enter http://server1/something in the address bar I get forwarded on to 192.168.0.1:8001/something. This is correct, but the address bar is wrong, showing http://server1:8001/something/

I don't know why it's added the port number in this instance.

The following is my rewrite rule
Code: Select all
    RewriteEngine on
    RewriteLog "C:/Program Files/Apache Software Foundation/Apache2.2/logs/proxy/rewrite.log"
    RewriteLogLevel 3
    RewriteCond %{HTTP_HOST} ^server1$ [NC]
    RewriteRule ^/(.*) http://server1:8001/$1 [L,P]

I'm aware that this may not be a rewrite problem, but wanted to check here first.

The rewrite log is showing an OK status for each request too. Here's what the log shows for both requests...
Code: Select all
192.168.0.1 - - [10/Jul/2009:17:41:26 +0100] [server1/sid#5a7a80][rid#2cf4120/initial] (2) init rewrite engine with requested uri /something
192.168.0.1 - - [10/Jul/2009:17:41:26 +0100] [server1/sid#5a7a80][rid#2cf4120/initial] (3) applying pattern '^/(.*)' to uri '/something'
192.168.0.1 - - [10/Jul/2009:17:41:26 +0100] [server1/sid#5a7a80][rid#2cf4120/initial] (2) rewrite '/something' -> 'http://server1:8001/something'
192.168.0.1 - - [10/Jul/2009:17:41:26 +0100] [server1/sid#5a7a80][rid#2cf4120/initial] (2) forcing proxy-throughput with http://server1:8001/something
192.168.0.1 - - [10/Jul/2009:17:41:26 +0100] [server1/sid#5a7a80][rid#2cf4120/initial] (1) go-ahead with proxy request proxy:http://server1:8001/something [OK]
192.168.0.1 - - [10/Jul/2009:17:41:35 +0100] [server1/sid#5a7a80][rid#2d04578/initial] (2) init rewrite engine with requested uri /something/
192.168.0.1 - - [10/Jul/2009:17:41:35 +0100] [server1/sid#5a7a80][rid#2d04578/initial] (3) applying pattern '^/(.*)' to uri '/something/'
192.168.0.1 - - [10/Jul/2009:17:41:35 +0100] [server1/sid#5a7a80][rid#2d04578/initial] (2) rewrite '/something/' -> 'http://server1:8001/something/'
192.168.0.1 - - [10/Jul/2009:17:41:35 +0100] [server1/sid#5a7a80][rid#2d04578/initial] (2) forcing proxy-throughput with http://server1:8001/something/
192.168.0.1 - - [10/Jul/2009:17:41:35 +0100] [server1/sid#5a7a80][rid#2d04578/initial] (1) go-ahead with proxy request proxy:http://server1:8001/something/ [OK]

If this is to do with the setup of the virtual host on the destination server then I can post that info too. Do I need some kind of a rewrite rule on the destination server?

Sorry for the long post.

Regards
Rich
huggie
 
Posts: 5
Joined: Tue Feb 17, 2009 10:18 am

Postby richardk » Fri Jul 10, 2009 10:14 am

I have multiple apache instances running on the same server.

Why are you using multiple Apache instances? Do they have different versions/configurations or do you just want them all on port 80? If it is the latter you should use VirtualHosts.

But if drop the forward slash, just enter http://server1/something in the address bar I get forwarded on to 192.168.0.1:8001/something. This is correct, but the address bar is wrong, showing http://server1:8001/something/

I don't know why it's added the port number in this instance.

The cause of the redirect is mod_dir on the 8001 instance. It doesn't know the request has been proxied, so is returning the URL it thinks is correct.

Setting the 8001 server's ServerName to server1:80 might fix the redirect.

The following is my rewrite rule

If you use mod_proxy you can have it replace the domain in some headers (ProxyPassReverse)
Code: Select all
<VirtualHost *:80>
  ServerName server1

  ProxyPass        / http://server1:8001/
  ProxyPassReverse / http://server1:8001/
</VirtualHost>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby huggie » Fri Jul 10, 2009 10:33 am

richardk wrote:Why are you using multiple Apache instances? Do they have different versions/configurations or do you just want them all on port 80? If it is the latter you should use VirtualHosts.

I'm only using one Apache install and the reason is that I'll eventually be running this on a Unix server where I won't have access to the root user who's the only one who can run Apache on port 80. I'll have the initial 'proxy' server setup then I won't be able to touch it. I'll also need to bounce Apache regularly on some of the instances and don't want the others affected.

Thanks for the reply, I'll look at your suggestions and see what I come up with. Then I'll post back.

Regards
Rich
huggie
 
Posts: 5
Joined: Tue Feb 17, 2009 10:18 am

Postby huggie » Fri Jul 10, 2009 1:12 pm

None of the earlier solutions worked. I managed to solve it by searching Google for the last three hours and it was a simple case of adding the following above the rewrite rules:
Code: Select all
    ProxyPreserveHost On

The problem I have now is that I don't understand why it's fixed it. I read the Apache documentation for ProxyPreservHost but I'm none the wiser. Surely /something isn't considered part of the host is it?

Any ideas?

Regards
Rich
huggie
 
Posts: 5
Joined: Tue Feb 17, 2009 10:18 am

Postby huggie » Fri Jul 10, 2009 1:37 pm

In addition to my last post, the other step you must take is to make sure that there's no port number on the ServerName directive.
huggie
 
Posts: 5
Joined: Tue Feb 17, 2009 10:18 am

Postby richardk » Sun Jul 12, 2009 2:50 pm

The problem I have now is that I don't understand why it's fixed it. I read the Apache documentation for ProxyPreservHost but I'm none the wiser. Surely /something isn't considered part of the host is it?

/something has already been removed by the normal proxy directives.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 110 guests

cron