nginx 通過cookie做分發

前言:因为日本客户的特殊需求(日本有些用户是将80系列端口屏蔽的只能访问443端口)需要对网站进行反向代理多个站点共用一个https协议、443端口,自己从来没了解这一块项目前期疯狂采坑,对于这个需求做做笔记记录一下过程

场景:有A  B  C三个网站,网站的文件结构名称类似,網站對應的地址A=http:test:80,B=http:test:8080,C=http:test:8081,並且三個網站的文件引用路徑都是相對地址以‘’\‘’開頭,而且各自引用的文件名稱大部分一樣但是文件內容各不相同

需求:通過訪問https:test/OA 能夠跳轉到地址A,訪問https:tests/SEDI能夠跳轉到地址B,訪問https:tests/OEDI能夠跳轉到C

第一步在三個網站的登錄頁面增加一個名為[ifWEB]的cookie,三個網站的cookie名稱必須一致用於後續做分發。

如圖:

第二步配置nginx:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    server {
        listen       443;
        server_name  https://tests;
        ssl on;  
        ssl_certificate D:/nginxnew-JP/SSL/elna.crt;  
        ssl_certificate_key D:/nginxnew-JP/SSL/elna.rsa;  
        ssl_session_timeout 120m;  
        ssl_protocols SSLv2 SSLv3 TLSv1;  
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
        ssl_prefer_server_ciphers on;  

        fastcgi_read_timeout 600; 
        fastcgi_send_timeout 600; 
        client_max_body_size 30m; 

    
        add_header Cache-Control no-cache;
        add_header Pragma no-cache;
        add_header Expires 0;
        
		#end

        #charset koi8-r;
        #access_log  logs/host.access.log  main;


		location /OA/
		{       
			root   html;
		        proxy_pass http://test:80/;
			proxy_pass_request_headers on;
		        if ( $host ~ "http://test:80/" )
			{
					rewrite https://tests/OA/$1 permanent;		
			}
			if ($document_uri ~ http://test:80/)
			{
					rewrite https://tests/OA/$1 permanent;		
			}
			if ( $Uri ~ "http://test:80/" )
			{
					rewrite ^/(.*)$ /OA/$1 permanent;		
			}
			
		}

                location /SEDI/
		{   
			root   html;
		        proxy_pass http://test:8080/;
			#proxy_redirect http://$host/;
			proxy_pass_request_headers on;
			if ( $host ~ "http://test:8080/" )
			{
				rewrite https://tests/SEDI/$1 permanent;		
			}
			if ( $host ~ "http://test:8080/" )
			{
				rewrite https://tests/SEDI/$1 permanent;		
			}
			if ($document_uri ~ http://test:8080/)
			{
				rewrite https://tests/SEDI/$1 permanent;		
			}
			if ( $Uri ~ "http://test:8080/" )
			{
			        rewrite https://tests/SEDI/$1 permanent;		
			}
		}

                location / {
			root   html;
			if ($http_cookie ~* "OA") {
			    rewrite   ^/(.*)$  /OA/$1 permanent;		
			}
			if ($http_cookie ~* "SEDI") {
                            rewrite   ^/(.*)$  /SEDI/$1  permanent;		
                        }
			if ($http_cookie ~* "OEDI") {
                            rewrite   ^/(.*)$  /OEDI/$1  permanent;		
                        }

		}

               
		location /OEDI/ 
		{
			root   html;
			proxy_pass http://test:8081/;
			#proxy_redirect http://$host/;
			proxy_pass_request_headers on;
			if ( $host ~ "http://test:8081/" )
			{
					rewrite https://tests/OEDI/$1 permanent;		
			}
			if ($document_uri ~ http://test:8081/)
			{
					rewrite https://tests/OEDI/$1 permanent;		
			}
			if ( $Uri ~ "http://test:8081/" )
			{
					rewrite https://tests/OEDI/$1 permanent;		
			}
		
		}
		
		

	}
}

在反向代理配置文件中增加如下指令,不緩存網站數據是因為這三個網站的文件名稱基本都是一樣的,如果設置成相同那麼會導致文件引用錯誤

add_header Cache-Control no-cache;
add_header Pragma no-cache;
add_header Expires 0;

以下為通過cookie做轉發的代碼,注:這裡三個被代理的網站一定要在打開登錄頁面的時候產生名稱相同值不同的cookie,否則無法通過cookie做轉發。

                location / {
			root   html;
			if ($http_cookie ~* "OA") {
			    rewrite   ^/(.*)$  /OA/$1 permanent;		
			}
			if ($http_cookie ~* "SEDI") {
                            rewrite   ^/(.*)$  /SEDI/$1  permanent;		
                        }
			if ($http_cookie ~* "OEDI") {
                            rewrite   ^/(.*)$  /OEDI/$1  permanent;		
                        }

		}

因為https://tests代理了http://test:80,http://test:8080,http://test:8081所以可以看成三個子網站組成了一個主網站,

所以三個子網站產的的cookie是共用的,因為都是處於同一個域中。所以當你打開/OA的時候ifWEB=OA,當你再次打開/SEDI的時候ifWEB=OA就會被/SEDI產生的ifWEB=SEDI覆蓋掉,這樣子就達到了通過cookie做轉發的功能,所以在cookie處理上三個網站都要產生名稱相同的cookie。

如果有類似的處理機制請留言討論,有什麼問題可以留言不一定能夠解答但是可以一起學習討論一下

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值