# Tomcat behind Nginx resulting in jsessionids in url

**URL:** <https://www.ruby-forum.com/t/tomcat-behind-nginx-resulting-in-jsessionids-in-url/223523>\
**Category:** NGINX\
**Created:** [October 8, 2012, 5:00pm UTC](https://www.ruby-forum.com/t/tomcat-behind-nginx-resulting-in-jsessionids-in-url/223523 "2012-10-08T17:00:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 8, 2012, 5:00pm UTC](https://www.ruby-forum.com/t/tomcat-behind-nginx-resulting-in-jsessionids-in-url/223523/1 "2012-10-08T17:00:50Z")

</div>

Hello everybody,

I’m trying to set up nginx as a reverse proxy and softwareloadbalancer.  
The backend is built by some tomcat servers.  
Using apache as a reverse proxy everything goes well, but using nginx  
the following occurs:

1.) With cookies turned off in the browser, links and urls look like  
this:  
[http://subdomain.example.com/mobile/details%3Bjsessionid%3DF42AFDC7C01E7295B29B143ED6F0772F;jsessionid=F42AFDC7C01E7295B29B143ED6F0772F?someParams=someValues](http://subdomain.example.com/mobile/details%3Bjsessionid%3DF42AFDC7C01E7295B29B143ED6F0772F;jsessionid=F42AFDC7C01E7295B29B143ED6F0772F?someParams=someValues)  
Somehow, the jsessionid is there twice, but once it is URL-Encoded.

With cookies enabled, links and urls look like this:  
[http://subdomain.example.com/mobile/details;jsessionid=F42AFDC7C01E7295B29B143ED6F0772F?someParams=someValues](http://subdomain.example.com/mobile/details;jsessionid=F42AFDC7C01E7295B29B143ED6F0772F?someParams=someValues)

Why is this happening?

2.) Some modules of the site are shown at least twice, again only while  
using nginx as reverse proxy. How did I achiev that?

Here is my configuration:

nginx.conf:  
user www-data www-data;  
worker\_processes 6;  
worker\_priority 0;  
error\_log /var/log/nginx/error.log;  
#log\_not\_found off;  
worker\_cpu\_affinity 000001 000010 000100 001000 010000 100000;  
events {  
multi\_accept off;  
worker\_connections 6144; #Muss eventuell an die Hardware  
angepasst werden. Erlaubt derzeit 6 \* 6144 gleichzeitige Anfragen)

}

http {

```
    log_format test_debug '$remote_addr - $remote_user [$time_local]

```

’ "$request " $status ’ Arguments: $args’;  
include /my/config/nginx/upstreams.conf;  
include mime.types;  
default\_type application/octet-stream;  
keepalive\_timeout 65;  
server {  
listen 80;  
server\_name localhost;  
location /nginx\_status {  
stub\_status on;  
access\_log off;  
allow 127.0.0.1;  
deny all;  
}

```
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root html;
            }
    }

```

# include /etc/nginx/conf.d/\*.conf;

```
    include /my/config/nginx/sites/*.conf;

```

}

proxy.conf:  
proxy\_set\_header Host $host;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_max\_temp\_file\_size 0;

proxy\_connect\_timeout 90;  
proxy\_send\_timeout 90;  
proxy\_read\_timeout 90;

proxy\_buffer\_size 4k;  
proxy\_buffers 4 32k;  
proxy\_busy\_buffers\_size 64k;  
proxy\_temp\_file\_write\_size 64k;

proxy\_pass\_header Set-Cookie;

upstreams.conf:

upstream service1 {  
ip\_hash;  
server 192.168.1.117:8080 weight=1 max\_fails=10  
fail\_timeout=60s;  
server 192.168.1.115:8080 weight=1 max\_fails=10  
fail\_timeout=60s;  
}  
upstream service2 {  
ip\_hash;  
server 192.168.1.117:7080 weight=1 max\_fails=10  
fail\_timeout=60s;  
server 192.168.1.115:7080 weight=1 max\_fails=10  
fail\_timeout=60s;  
}

/my/config/nginx/sites/service1.conf:

server {  
error\_log subdomain\_error.log;  
access\_log subdomain\_access.log test\_debug;  
rewrite\_log on;  
include /my/config/nginx/proxy.conf;  
listen 80;  
server\_name [subdomain.example.com](http://subdomain.example.com);  
rewrite “^/([^%]+)[^;]_([^?]+)?(?{0,1}._)” /$1$2$3; #used to  
kick out the wrong jsessionid befor passing the request, avoiding a 404  
if ($http\_cookie ~\* “jsessionid=([^;]+)(?:;|$)”) {  
set $co “jsessionid=$1”;  
}  
location = / {  
proxy\_set\_header Cookie $co;  
proxy\_pass [http://service1/?param=value](http://service1/?param=value);  
}

```
    location ~* "^/(images|css|js|userimages)/(.*)$" {
            proxy_pass http://service1/$1/$2;
    }

    location ~* "^/(.*\.(jpg|gif|png))$" {
            proxy_pass http://service1/$1?;
    }

    location / {
            proxy_set_header Cookie $co;
            proxy_pass http://service1;
    }

```

}

server {  
error\_log /var/log/nginx/subdomain\_ssl\_error.log;  
rewrite\_log on;  
listen 443 ssl;  
server\_name [subdomain.example.com](http://subdomain.example.com);  
ssl\_certificate  
/raumo/config/nginx/zertifikate/subdomain.example.com.crt;  
ssl\_certificate\_key  
/raumo/config/nginx/zertifikate/subdomain.example.com.key;  
if ($args\_param !~ “value”){  
set $args\_param “value”;  
}  
location = / {  
proxy\_set\_header X-Forwarded-Host $host;  
proxy\_set\_header X-Forwarded-Server $host;  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for;  
proxy\_pass [http://service1](http://service1);  
}  
location / {  
proxy\_set\_header X-Forwarded-Host $host;  
proxy\_set\_header X-Forwarded-Server $host;  
proxy\_set\_header X-Forwarded-For  
$proxy\_add\_x\_forwarded\_for;  
proxy\_pass [http://service1](http://service1);  
}  
}

server{  
error\_log /var/log/nginx/subdomain\_redir\_error.log;  
listen 80;  
server\_name [example.com](http://example.com);  
location / {  
return 301 [http://subdomain.example.com](http://subdomain.example.com);  
}  
}

server{  
listen 80;  
server\_name [www.subdomain.com](http://www.subdomain.com);  
location / {  
return 301 [http://subdomain.example.com](http://subdomain.example.com);  
}  
}

Would apreciate any help.

Thank you all in advance

T.Hipp
