It's not a secret anymore!*
The moose likes Glassfish and the fly likes Glassfish: why is a trivial URL redirected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Glassfish
Bookmark "Glassfish: why is a trivial URL redirected?" Watch "Glassfish: why is a trivial URL redirected?" New topic
Author

Glassfish: why is a trivial URL redirected?

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I've got an application that I'm running on Tomcat6 in production, but developing and debugging using Glassfish and Netbeans.

I start out with the simpliest URL possible:
http://localhost:8080/webapi/

since Apache is running on 80, I have glassfish using its default to run on port 8080

"webapi" is the webapp/context

When I run it though HTTPfox, I can see that this is redirected, rather than being simply responded to.



The only thing I can see is that the redirected page has a trailing slash.

What is the significance of this redirect?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Pat,

if I've understood the description of your "experiment" with HTTPfox correctly the URL you are trying to access is not the one you posted but instead without the trailing slash?!?

Then the redirect would be correct and furthermore it would be what most (almost all?) web servers will do in that case. That's because the correct URL for a directory (which the root of your application context is) should end with a slash! The Apache HTTP server will do the same if you are trying to access a directory like ".../webapi/" (in contrast to a file) and omit the trailing slash. It's just that the end user most of the time is not aware of such redirects because modern browsers and web servers take care of this behind the scenes. Only with a sniffer like HTTPfox you can see what is really going on.

I hope this explains your redirect. Oh, here are some of the first links from Google that match the problem. As you can see it's a popular topic for confusion ;-)

Google Webmaster Central Blog
Another blog

Marco
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Correct, I typed it without the trailing slash.

How can I tell which did the rewrite? I can think of three possibilities:
1) the browser
2) apache
3) glassfish

Any idea as to which is doing it?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

3) GlassFish

Redirects are always sent from the server to the client! Apache has nothing to do with this as GlassFish is running in standalone mode accordingly to your description i.e without Apache httpd as a proxy. Using the said URL you are connecting directly to GlassFish on port 8080 and the Servlet container (= Tomcat) inside the GlassFish server is sending a redirect response to your browser!

Marco
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

So while debugging, Apache httpd has nothing to do with it. OK, I'm cool with that.

But in production, where I'm using mod-proxy in Apache httpd to talk to Tomcat6, then it could still be either httpd or tomcat6.
(I see the same action in production, its just a lot harder to set breakpoints in that setup.).

To the browser, "the server" is really both, as mod-proxy is just being, duh, a proxy.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Yes, if you are working with an Apache httpd reverse proxy things are a little bit more complicated. In the end what clients see depends on the configuration and action of both, the frontend and the backend server. And as especially Apache httpd is *VERY* configurable it isn't always easy to tell what is happening without knowing the configuration of the whole system in detail.

In a usual setup there will definitely be redirects coming from Tomcat AND httpd! For example httpd will have to rewrite location headers containing the URL of the backend server to avoid that httpd may get bypassed for some requests. Other redirects, like a redirect to https for secure areas for example, are usually initiated by Tomcat (or GlassFish) and just forwarded by httpd to the browser.

But for debugging the origin of a redirect, it should be enough to inspect the access log files which httpd, Tomcat and GlassFish provide (when configured accordingly). If a redirect response is sent by Tomcat or GlassFish it will appear in its access log together with the corresponding HTTP status code (see this article on Wikipedia). Otherwise it will only be logged by the httpd server. Of course it may be difficult to get the logs of a production server but if there is a real problem with this, someone should be able to help you to debug this problem.

There's just one question what is puzzling me. What is wrong with these redirects? It's just the way HTTP has worked for years and there are good reasons for this. Clients like browsers can easily make such redirects completely transparent to the end user and as a human user you usually don't even notice most of these simple redirects. Is it just out of curiousity or do you have a real problem with this?

Marco
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Marco Ehrentreich wrote: Is it just out of curiousity or do you have a real problem with this?


I have a real problem, but not with this simple case. I need to understand what is doing the redirects in a case that causes real problems.

I posted it to stackoverflow two days ago, with no answers. http://stackoverflow.com/questions/11478642/how-can-i-find-out-which-module-is-doing-a-redirect

The problem is that some times, "something" does a redirect with a 302 moved permanently, to a URL that can't exist.

http://my.wayfinderdigital..com/webapi/command/foo

It puts two periods between the domain and the TLD. I have no idea what is doing it. Could be Apache httpd, could be tomcat6, could be my code, could be something I'm not thinking of.
Its driving me crazy and is a real problem because users are seeing the ".." and broken URL
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Can you tell a little bit more about the setup which causes this problem? Does it only happen in production or can you reproduce it in a development environment? With or without Apache front-end? Maybe it's just a wrong hostname in a server (Apache/GlassFish/Tomcat) configuration which is used to construct canonical URLs like the ServerName parameter for Apache httpd. Or a wrong DNS configuration? Does a "nslookup" resolve your hostname and IP correctly in both directions, i.e. forward and reverse? Did you have a look at the server configuration files?

I don't think these strange URLs are constructed by your application, because the .. is inside the host part of the URL. So I suspect it's something in the server's configurations, but it's hard to tell without knowing more details...

Marco
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Marco Ehrentreich wrote:Does it only happen in production or can you reproduce it in a development environment?


I can't cause it to reproduce in production. I've never seen in in development. But it does happen in production and then it will happen for a while.

With or without Apache front-end? Maybe it's just a wrong hostname in a server (Apache/GlassFish/Tomcat) configuration which is used to construct canonical URLs like the ServerName parameter for Apache httpd. Or a wrong DNS configuration? Does a "nslookup" resolve your hostname and IP correctly in both directions, i.e. forward and reverse? Did you have a look at the server configuration files?


Its always with Apache httpd as the front-end.

Apache config:

<VirtualHost *:80>
ServerAdmin pfarrell@wayfinderdigital.com
ServerName wayfinderdigital.com
ServerAlias *.wayfinderdigital.com wayfinderdigital.com
ServerAlias *.wyfn.us wayfinderdigital.com

Of course there could be something wrong with the DNS setup, DNS is a black art. But the forward resolution works properly.

dig my.wayfinderdigital.com

; <<>> DiG 9.7.3 <<>> my.wayfinderdigital.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65013
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 2

;; QUESTION SECTION:
;my.wayfinderdigital.com. IN A

;; ANSWER SECTION:
my.wayfinderdigital.com. 900 IN A 184.180.159.168

Never setup the reverse side, my ISP answers that with a generic answer


I don't think these strange URLs are constructed by your application, because the .. is inside the host part of the URL. So I suspect it's something in the server's configurations, but it's hard to tell without knowing more details...


That was my thinking, my app doesn't even know its own host name, it all works relative or gets the hostname from the HTTP request.

You suggested looking at the logs. I don't see anything weird in the logs, but perhaps I need to raise some log level. Any hints as to which?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Very strange!

But this snippet of the Apache configuration really does look correct to me. What's the difference between your whole setup in production and development? Is there perhaps a load balancer and a cluster of Apaches in production with only one of them accidentally misconfigured?

I've also tested if it's possible to manipulate DNS names in such a way but MaraDNS doesn't even allow me to load an invalid configuration with .. in the domain name. Not sure about more popular DNS servers like Bind but I suspect it does some validation, too.

I'm sorry, but I don't have any more ideas at the moment what could cause the problem. I would look closely at the access logs of both Apache httpd and your backend server. If a client/browser tries to follow these bad redirect URLs it should show up in any access log. This should at least help you to figure out where the redirects happen (front-end vs. back-end) and maybe you can narrow the problem down to some specific parts of your application.

Good luck!

Marco
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9924
    
158

Pat Farrell wrote:

The problem is that some times, "something" does a redirect with a 302 moved permanently, to a URL that can't exist.

http://my.wayfinderdigital..com/webapi/command/foo

It puts two periods between the domain and the TLD. I have no idea what is doing it. Could be Apache httpd, could be tomcat6, could be my code, could be something I'm not thinking of.
Its driving me crazy and is a real problem because users are seeing the ".." and broken URL


That looks like some kind of URL shortening module (mod_rewrite?) coming into picture which shortens long URLs. That might also explain why this doesn't always happen since the shortening of the URLs is decided by the length of the original URL.

[My Blog] [JavaRanch Journal]
 
 
subject: Glassfish: why is a trivial URL redirected?
 
Similar Threads
Help Running GlassFish bundled with Netbeans, what does Apache & IIS redirect have to do with this?
Debugging Eclipse
Where does the <Connector> config information go?
tomcat6: Can't resolve The absolute uri: http://java.sun.com/jsp/jstl/core
Remote Debugging in Eclipse