2

I use nginx as front-end server and tomcat as back-end. The clients access my site via https scheme. For example:

https://example.com/app/test

Nginx receive this and pass the request to tomcat via this local address:

http://localhost:8080/app/test

My nginx config:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forward-For $remote_addr;
    proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forward-Proto $scheme;
    proxy_pass http://localhost:8080;
}

This works fine. However, when I try to use redirect (http 302), it always turn https to http. This is my redirect code:

response.sendRedirect("/app/redirect-test");

What I expecting is to redirect the browser to:

https://example.com/app/redirect-test

but it always redirects the browser to:

http://example.com/app/redirect-test.

I also log the headers to track this issue:

host : example.com
x-real-ip : 180.168.202.246
x-forward-for : 180.168.202.246
x-forward-proto : https
connection : close
......

But I still cannot figure out. And I try to log request.getRequestURL(), then I get this result:

http://example.com/app/test

How can I solve this problem?

1
  • I realized I make a mistake, X-Forward-For should be X-Forwarded-For and X-Forward-Proto should be X-Forwarded-Proto. But after I fixed that, the issue still exists. Commented Dec 5, 2014 at 11:19

1 Answer 1

4

I got the solution.

nginx.conf

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://localhost:8080;
}

Modify Tomcat's server.xml Host section

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- blah blah blah -->
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
</Host>

Go to this place to see the detail: Tomcat Doc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.