nginx根据url参数动态代理

nginx根据url参数动态代理

请求url格式,其中参数proxy后面的url就是需要访问的真实地址:
http://localhost:9388/?proxy=http://localhost:8038/Content/layui/font/iconfont.woff?v=256
http://localhost:9388/?proxy=http://localhost:8072/article/ArticleInfo/12309f4e71be45bf943f5b1346162e42
http://localhost:9388/?proxy=http://localhost:8038/user/login
http://localhost:9388/?proxy=http://99.89.11.137:8833/

windows版本的nginx:

server {
		listen  9388;
		server_name  localhost;	
	 	 resolver   192.168.2.1;    # 配置DNS
	 	# resolver   192.168.2.234;	  
		
			# 申明变量
			set   $realHost    "333";		 
			set 	$backend_host   "333";	
 
		#获取url的域名
	 	if ($arg_proxy ~ ^(http(s)?://)?([^/?]*)(.*)?$ ){
	 		set 	$backend_host	 $3;
	 	}
					
					#解析真实后台地址域名,针对于请求css、js文件 
					if ($http_referer   ~*     proxy=(http(s)?://)?([^/?]*)(.*)?$	){
				 				set   $realHost     $3;
				 	}

		 	        #  当下载css文件里面的相对路径文件时,会出现500错误	
					# 比如http://localhost:9388/Content/layui/font/iconfont.woff2?v=256		
				    if (	$realHost   =   "333"	){
						 set   $realHost     $cookie_cookieHost;
					} 
			 
	 	location  /  {	
			  	proxy_set_header 	Host	 						 $backend_host;
	 			proxy_set_header    X-Real-IP   	 		     $remote_addr;
	 			proxy_set_header    X-Forwarded-For   	 $remote_addr;
			    proxy_set_header		whl_host	 			     $backend_host;			    
							 
			  #自定义响应头
			   add_header 		whl_host 	 $backend_host; 
			   add_header 		whl_url 	 $arg_proxy; 		 
		
		
						# 请求图片、css、js		 
						if  (  $http_Sec_Fetch_Dest   ~*   (image|script|style|font)   	)
						{						
								add_header 		whl_file_host 	 $realHost; 
								add_header 	    whl_file_url	     $uri;	
								
								proxy_pass	  http://$realHost;  	#	有效
								break; 
						}
						
					#  接口请求
					if ( $http_X_Requested_With   ~*   XMLHttpRequest  )
					{			
						add_header 		whl_XMLHttpRequest_host 	 $realHost; 
						add_header 	    whl_XMLHttpRequest_url	     $uri;	
						
						proxy_pass	   http://$realHost;	 	
						break; 				  
					}		
			  
			      # 初次请求页面document
				  if ( $arg_proxy ){					  
					  add_header   	Set-Cookie 	 'cookieHost=$backend_host;path=/;';		
					  add_header 	 whl_arg_proxy	     $arg_proxy;	
						
					 proxy_pass	   	$arg_proxy;	 
					 break; 					 
				  }
			  
			       proxy_pass	   http://$realHost;	 	 				 
	 	}		 
		
		 
	#		 	# location   ~* \.(js|css|gif|jpg|jpeg|jfif|png|bmp|swf|ico|woff2|woff|ttf|mp4|flv)$
	#			 location   ~* \.(js|css|swf|woff2|woff|ttf|mp4|flv)$
	#		 	 {		 
	#		 		proxy_next_upstream 	http_502 	http_504 	error 	timeout 	invalid_header;
	#		 		proxy_set_header		whl_host	 				$backend_host;
	#		 		proxy_set_header		whl_url	 	 				$arg_proxy;
	#		 		proxy_set_header  	Host  						$backend_host;
	#		 		proxy_set_header 	X-Forwarded-For 		$remote_addr;
	#				proxy_set_header     X-Real-IP   	 		 	$remote_addr;
	#		 		
	#		 		  #自定义响应头				
	#		 			add_header 	whl_host 	 					$backend_host; 
	#		 			add_header 	whl_url 	 					$arg_proxy; 
	#		 			add_header 	whl_req_url					$query_string; 
	#		 			add_header 	whl_http_referer			$http_referer; 
	#		 			add_header 	whl_real_host					$realHost; 		 		 	 
	#		 		 	add_header 	whl_get_cookieHost			$cookie_cookieHost; 
	#		 		 	add_header 	whl_http_Accept			$http_Accept; 
	#					add_header   	whl_Sec_Fetch_Dest 	 $http_Sec_Fetch_Dest;			    
	#		 			
	#		 	     proxy_pass			 http://$realHost;  #有效
	#					  
	#		 	  }	 

}

你可能感兴趣的:(负载均衡器nginx,nginx,运维)