4

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.

Cesar
  • 137
  • 1
  • 1
  • 6
  • Try adding `proxyPort="80"` under `Connector` directive in Tomcat `server.xml` file – krisFR Aug 09 '14 at 22:24
  • as @krisFR, you need to tell Tomcat that it is running behind a proxy, otherwise it will generate wrong redirection and link urls. On the other hand, what do you mean by "it is not keeping the session"? – Florin Asăvoaie Aug 10 '14 at 07:19
  • I get a new session id on every request – Cesar Aug 10 '14 at 19:43

4 Answers4

11

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/;
}
  • This solution worked for me too. After a lot of google search I finally found this post and solve all my problems. In my case, it was an old Flash application that send jsessionid over the URL in format mysite.com;jsessionid=8374929fsdfs, NGINX got errors about "bad HTTP format" and never save the session. Just adding *proxy_cookie_path* in that way, fix the problem. – Eagle Jan 30 '19 at 11:35
1

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.

Alvaro
  • 11
  • 1
0

You can enable sticky session or session affinity. This will ensure that the requests from same client are passed to the same tomcat every time

Change in nginx:

sticky cookie srv_id expires=1h domain=.example.com path=/;

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky

skonka
  • 91
  • 1
  • 4
-2

I am also facing the same issue that is unable to keep session. Cesar has mentioned that he has resolved the issue with apache but I am getting the same even with apache. Can someone pls help me. I have did the following setup.

DNS Configuration

subdomain.mydomain.com 10.10.10.10

Proxy configuration in apache at 10.10.10.10

<VirtualHost *:80>
ServerName subdomain.mydomain.com

ProxyPass / http://tomcatserverIP:8080/appname
ReverseProxyPass / http://tomcatserverIP:8080/appname

</VirtualHost>