Session lost in tomcat uing nginx as proxy
I have several applications in a tomcar server. I am using nginx for proxy so i can achieve acceding the application from a subdomain
tomat:8080/app1 > app1.mydomain.com
tomat:8080/app2 > app2.mydomain.com
I set up a reverse proxy:
server {
listen 80;
server_name app1.mydomain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_pass http://tomcat:8080/app1/;
}
}
I can acces the application without problem. But it is unable to keep the session. I have another appliction in jetty that runs without problem behind the ngix. Also if i acces the application directly i can operate it without problem.
Any hint?
Thanks.
Solution 1:
I noticed the issue occurs when the Proxy location path does not match the Tomcat application context name and there is a cookie path mismatch which causes a new JSESSIONID for every request.
Try adding the proxy_cookie_path directive as mentioned below:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_cookie_path ~*^/.* /;
proxy_pass http://tomcat:8080/app1/;
}
Solution 2:
For me, the answer provided by Diwakar Timilsina has been the exact one which solved my problem..
Having nginx responding to the root context of the VHost, and proxying the requests towards an instance of Tomcat in a context /application
For example:
http://hunt.pepe.com/index.jsp --> http://localhost:8080/hunt-app
The only directive I added was [ proxy_cookie_path ~^/. /; ]:
location / {
·
·
proxy_cookie_path ~*^/.* /;
·
proxy_pass http://localhost:8080/hunt-app;
}
Thanks a lot dude, you literally saved my ass.