Tomcat behind Nginx resulting in jsessionids in url

Tobias Hipp t.hipp at raumobil.de
Mon Oct 8 15:00:08 UTC 2012


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
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

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;
        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;
        }

        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;
        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;
        }
        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;
        }
}

server{
        error_log /var/log/nginx/subdomain_redir_error.log;
        listen 80;
        server_name example.com;
		 location / {
                return 301 http://subdomain.example.com;
        }
}

server{
        listen 80;
        server_name www.subdomain.com;
        location / {
                return 301 http://subdomain.example.com;
        }
}

Would apreciate any help.

Thank you all in advance

T.Hipp



More information about the nginx mailing list