Showing posts with label web application. Show all posts
Showing posts with label web application. Show all posts

Tuesday, January 24, 2017

Too Many Redirect with SSL in NGINX and Jetty Server

Recently I faced a very nasty problem.

I am using Nginx as my web server and Java Web Application as App Server hosted in Jetty.

I was required to host all the pages using SSL i.e. HTTPS. I enabled Nginx to handle the HTTPS requests at the port 443. Nginx did handle the https requests however Jetty server received all the HTTPS requests as HTTP. There I had the logic to redirect all HTTP to HTTPS. So I fell into the problem that it was redirecting again and again, therefore causing Too Many Redirect error.

I had to spent a lot of time and finally I found the error and hence solution.

While the problem was HTTPS scheme was getting lost at load balancer. This could be solved using the following snippet into the web.xml


  <context-param>
    <param-name>org.mortbay.jetty.servlet.SessionPath</param-name>
    <param-value>/</param-value>
  </context-param>

  <filter>
    <filter-name>XForwardedFilter</filter-name>
    <filter-class>fr.xebia.servlet.filter.XForwardedFilter</filter-class>
    <init-param>
      <param-name>protocolHeader</param-name>
      <param-value>x-forwarded-proto</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>XForwardedFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>