使用nginx&和多个域共享相同的会话tomcat的

时间:2014-09-08 16:03:09

标签: session tomcat grails nginx

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

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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 xyz.com www.xyz.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 *.xyz.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 *.xyz.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.xyz.com b.xyz.com ,工作正常并共享同一会话。< / p>

但我们需要使用相同的会话和应用使用不同的域名,如: abc.com ,我该如何实现?我尝试过设置虚拟主机和 proxy_cookie_domain 但没有任何效果?

0 个答案:

没有答案