1

I have this configuration in place:

haproxy(80,443) --> httpd(8443) --> tomcat(8096)

Going to https://websrv1/test will result in a 301 redirect to the httpd port for which haproxy should be proxying requests. So the user is redirected to http://websrv1:8443/test.

But, if the user goes to https://websrv1/test/ - specifying a the trailing slash - it works just fine.

How can I prevent this redirection from happening, when the user DOES NOT specify a trailing slash?

  • haproxy configuration?
  • apache url rewrite?
  • tomcat url rewrite?

2 Answers 2

2

The problem is on the Apache level.

You could try to set

UseCanonicalName On

and

ServerName websrv1:80

in your Apache config to make apache use the correct rewrite.

Alternatively you could disable the rewrite of the trailing slash with

DirectorySlash Off

See UseCanonicalName, ServerName and DirectorySlash

4
  • Thanks. I tried all the options that you suggested and the problem persists.
    – BlackBeret
    Aug 6, 2013 at 7:10
  • It is possible that your browser has the redirects cached because the are set with a permanent redirect Aug 6, 2013 at 10:40
  • Nope.. I tried after cleaning all caches.
    – BlackBeret
    Aug 6, 2013 at 10:54
  • I don't know if HAProxy caches rewrites. But that might be possible. Aug 6, 2013 at 11:13
0

I know this is an old post, but since I was struggling with this even in 2024 with tomcat 10.1 and tried too many options before it finally worked I decided to post what I've found.

TLDR:

Set scheme="https" in your <Connector> block. The issue originates from Tomcat.

My road to this solution

I had only found this question and one other: Tomcat Trailing Slash With Proxy Forwarding but not much more on the topic.

Since we also have a mix of nginx/tomcat I couldn't find out who was doing the https to http redirect. I'll show what I've tried, they may work in your case if the above solution doesn't work.

  • Attempt 1: Add proxyPort="443" to the <Connector> block. Failed in a miserable redirect to http://hostname:443/path
  • Attempt 2: Added mapperContextRootRedirectEnabled="false" mapperDirectoryRedirectEnabled="false" to the <Context> block. Also tried with true but sadly nothing changed.
  • Attempt 3: Set useRelativeRedirects="true" in the <Context> block. While this sounded very useful and actually worked when doing a request directly to tomcat (without nginx) giving a redirect /path. This does not seem to work when requesting via nginx. I really hope I figure this out some day.
  • Attempt 4: Making sure it was really Tomcat's fault because the above setting worked locally. By setting sendRedirectBody="true" in the <Context> block made it obvious that it was tomcat since the response body now also contained the faulty redirect url.
  • Attempt 5: Reading through all the options in https://tomcat.apache.org/tomcat-10.1-doc/config/http.html#Common_Attributes hoping to find something useful. As stated before this made me change scheme="https" in the <Connector> block and the issue was gone.

Hope this saves someone else a headache.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .