本项目完成的任务,nginx1.9.2+tomcat8 配置 多tomcat负载均衡,并使静态文静不通过tomcat解析,而是直接通过nginx直接返回静态文件数据,提高响应时间,提高并发量。注意,在nginx.conf设置静态文件路径时,要确保此路径在本用户下有访问权限,最好设为chmod 7777 filename。
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; upstream lhy { #配置两台tomcat负载均衡,ip:port地址为tomcat地址。weight为权重,权重越大,访问概率越大, server 192.168.1.106:8080 weight=10; server 192.168.1.106:8090 weight=10; } server { listen 80; server_name localhost; #charset koi8-r; # access_log logs/host.access.log main; location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /home/lhy/nginx_data/; #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 7d; } location ~ (\.jsp)|(\.do)$ { #root html; #index index.jsp index.htm; #lhy是 upstream 后面的名字 lhy proxy_pass http://lhy; #localhost是nginx服务器的主机地址,如果不写此句,会导致静态文件访问路径为http://lhy,导致找不到地址 proxy_set_header Host localhost; #forwarded信息,用于告诉后端服务器终端用户的ip地址,否则后端服务器只能获取前端代理服务器的ip地址。 proxy_set_header Forwarded $remote_addr; } # / 表示匹配所有地址,默认最大前缀匹配,如果其他没有匹配的才会匹配 location /{ root /home/lhy; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #错误页面地址,500 502 503 504错误的地址 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
路径转发
server { listen 80; server_name 115.28.77.24;#本服务器ip地址 #charset koi8-r; #access_log logs/host.access.log main; # location /vpn/ { location /{ #root html; #index index.html index.htm; proxy_pass http://10.8.0.66:8090/; #当地址最后加上 /时,匹配路径的 yanshi 不会加到转发路径中 proxy_set_header X-Forwarded-For $remote_addr; #当下面这句话不加,Host $host; 会导致post请求参数丢失 proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; }
}
同一服务器部署多个tomcat时,存在端口号冲突的问题,所以需要修改tomcat配置文件server.xml,以tomcat7为例。
- 首先了解下tomcat的几个主要端口:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="60000" redirectPort="8443" disableUploadTimeout="false" executor="tomcatThreadPool" URIEncoding="UTF-8"/>
其中8080为HTTP端口,8443为HTTPS端口
<Server port="8005" shutdown="SHUTDOWN">
8005为远程停服务端口
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
8009为AJP端口,APACHE能过AJP协议访问TOMCAT的8009端口。
- 部署多个tomcat主要修改三个端口:
1.HTTP端口,默认8080,如下改为8081
[html] view plain copy
- <Connector port="8081" protocol="HTTP/1.1"
- connectionTimeout="60000"
- redirectPort="8443" disableUploadTimeout="false" executor="tomcatThreadPool" URIEncoding="UTF-8"/>
2.远程停服务端口,默认8005,如下改为8006
[html] view plain copy
- <Server port="8006" shutdown="SHUTDOWN">......
3.AJP端口,默认8009,如下改,8010
[html] view plain copy
- <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
nginx与tomcat的结合,主要用的是nginx中的upstream,后端可包括有多台tomcat来处ginx的upstream目前支持4种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstreambakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session 的问题。
例如:
upstreambakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
serverserver1;
serverserver2;
fair;
}
4、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
serversquid1:3128;
serversquid2:3128;
hash $request_uri;
hash_methodcrc32;
}
tips:
upstream bakend{#定义负载均衡设备的Ip及设备状态
ip_hash;
server127.0.0.1:9090 down;
server127.0.0.1:8080 weight=2;
server127.0.0.1:6060;
server127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲clientpost过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡