0

We've a running a grails application on a tomcat server behind nginx for multiple subdomain:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
a
    log_format  main  '$remote_addr - $remote_user [$time_local] "$tempRequest" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" \n';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_http_version   1.1;
    gzip_min_length     1000;
    gzip_buffers        16 8k;
    gzip_disable        "MSIE [1-6] \.";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
    gzip_vary           on;

    upstream main {
        server localhost:8081;
    }

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

    # First server config to listen top level domain request (https/http) & redirect to mnop.com
    server {
        listen 80;
        listen 443 ssl;
        server_name example.com www.example.com;
        ssl_certificate /etc/nginx/server.crt;
        ssl_certificate_key /etc/nginx/server.key;
        return 301 https://mnop.com;
    }

    # Second server config to redirect all non https requests to https request.
    server {
        listen 80;
        # Remember wildcard domain with "*" doesn't listen top level domain.
        # Hence no conflict with first server config.
        server_name *.example.com;
        rewrite  ^ https://$host$request_uri? permanent;
    }

    # Third server config to listen https request & serves all html with nginx.
    server {
        listen 443 ssl;
        server_name *.example.com;
        ssl on;
        ssl_certificate /etc/nginx/server.crt;
        ssl_certificate_key /etc/nginx/server.key;
        ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        ssl_session_cache shared:SSL:10m;
        location / {
            set $tempRequest $request;
            if ($tempRequest ~ (.*)j_password=[^&]*(.*)) {
                # Mask spring authentication password param.
                set $tempRequest $1j_password=****$2;
            }
            proxy_set_header  Host $http_host;
            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://main;
            proxy_redirect http://$host https://$host;
        }
        location /ng/app {
            root /usr/share/apache-tomcat-7.0.54/webapps/ROOT;
        }
    }
}

A tomcat app is running on port 8081 and any subdomain like: a.example.com or b.example.com, working fine and sharing same session.

But We need to use the same session and app using a different domain like: abc.com, how can I achieve that? I tried setting virtual hosts and proxy_cookie_domain but nothing worked?

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47

1 Answers1

0

By spec, cookies can't be shared between domains (between domain and subdomains, it is possible)

Some workaround involve using a "master" domain and "slave" ones. User logs on on master domain, gets a cookie then navigates to a slave domain. There, slave asks master, on behalf of user, if a cookie exists. If so, as it is only server side, cookie can be replicated.