tomcat8+nginx动静分离、负载均衡(2)
时间:2016-11-06 17:52 来源:潇湘夜雨 作者:华嵩阳 点击:次
server_name 172.24.69.127;
root /usr/local/html/;
index index.htmlindex.htmindex.jsp;
location / {
proxy_pass http://tomcats;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
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;
}
}
为了方便查看后端负载的情况可以编辑nginx日志格式:
在httpd模块添加:
log_format access '$remote_addr:$remote_port|$http_x_forwarded_for $time_local $server_protocol $request_method $host$request_uri |$status| $body_bytes_sent "$http_referer" "$http_user_agent" $upstream_addr $upstream_response_time $upstream_status';
access_log /var/log/nginx/access.log access;
传递相关变量:
add_header proxy-Server $upstream_addr; #传递后端服务器ip地址
add_header x-server $hostname; #传递服务器主机名
浏览器中报头信息:
proxy-Server:172.18.109.239:8080
Server:nginx/1.10.1
Transfer-Encoding:chunked
x-server:www.test.com
其中,upstream 节点里面配置的是每个参与负载均衡的tomcat的ip, weigth 参数表示权值,权值越高被分配到
的几率越大,如果每台机器的weight=1,表示每台机器被分配到的概率是一样的。tomcats是upstream配置项的名称,可以自定义,但是一定要和locatin节点里面的 proxy_pass 配置一样
至此,我们已经把动静分离和负载均衡配置好了。
|