【原创】Nginx反向代理根据url参数访问后端指定服务器

今天因为工作需要,需要指定图片访问的是后端指定的反向代理服务器。
最后修改nginx配置实现,访问效果//img.netpc.com.cn/1.jpg?server=100或者//img.netpc.com.cn/1.jpg?server=101
配置大家自己看就好了,不懂留言。

upstream images {
	ip_hash;
	server 192.168.0.100:8008 weight=5;
	server 192.168.0.101:8008 weight=5;	
}
server {
	listen       80;
	server_name  img.netpc.com.cn;
	#charset utf-8;

	access_log  /data/logs/nginx/cluster.img.log;

	location / {
		#limit_req zone=ddos burst=30 nodelay;
		
		set $server 0;
		if ($query_string ~* "server=(\d+)$") {
			set $server $1;
		}
		if ($server = "100"){
			proxy_pass http://192.168.0.100:8008;
		}
		if ($server = "100"){
			proxy_pass http://192.168.0.101:8008;
		}
		if ($server = "0"){
			proxy_pass http://images;
		}
		
		proxy_redirect off;    
		proxy_set_header Host $host;    
		proxy_set_header X-Real-IP $remote_addr;    
		proxy_set_header SSL_CERT $ssl_client_cert;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
		client_max_body_size 10m;    
		client_body_buffer_size 128k;    
		proxy_connect_timeout 90;    
		proxy_send_timeout 90;    
		proxy_read_timeout 90;    
		proxy_buffer_size 4k;    
		proxy_buffers 4 32k;    
		proxy_busy_buffers_size 64k;    
		proxy_temp_file_write_size 64k;
		#proxy_ignore_headers "Cache-Control" "Expires";
		#proxy_pass_header Set-Cookie; 
		#expires 7d;
		#proxy_http_version 1.1;
	}
}
此条目发表在服务器分类目录,贴了标签。将固定链接加入收藏夹。

发表评论