使用 nginx 和 tomcat 与多个域共享同一会话

使用 nginx 和 tomcat 与多个域共享同一会话

我们在 nginx 后面的 tomcat 服务器上运行了一个用于多个子域的 grails 应用程序:

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

tomcat 应用程序在端口 8081 和任何子域上运行,例如:a.example.com或者b.example.com,运行良好并共享同一会话。

但是我们需要使用不同的域的相同会话和应用程序,例如:美国广播公司,我该如何实现?我尝试设置虚拟主机,proxy_cookie_domain但什么都没起作用?

答案1

根据规范,cookie 不能在域之间共享(在域和子域之间,这是可能的)

一些解决方法涉及使用“主”域和“从”域。用户登录主域,获取 cookie,然后导航到从属域。在那里,从属域代表用户询问主域是否存在 cookie。如果存在,由于它只是服务器端,因此可以复制 cookie。

相关内容