ngnix+tomcat转发、负载均衡
@TOC
前言
使用ngnix转发所有服务的网址到各个服务器,然后在各个服务器同时部署多个服务。
一、流程及图
1.简易流程图如下
一、nginx服务器配置:
1.下载并安装nginx
1.1 将下载好的nginx包传到服务器/usr/local/目录下
# cd /usr/local
// -y表示遇到判断全部yes,autoconf表示自动配置,automake表示自动编译。
# yum -y install gcc gcc-c++ autoconf automake //gcc、gcc-c++的库文件
# yum install -y pcre pcre-devel //安装Nginx依赖包
# yum install -y zlib zlib-devel
# tar -zxvf nginx-1.10.2.tar.gz //解压缩
# cd nginx-1.10.2 //切换到该目录下
# ./configure //配置
# make
# make install //安装
// 检验是否完成安装
# cd /usr/local
# ls //如果存在nginx文件夹,则安装成功
// 注意:最后结束前不要去改解压后的nginx文件,否则安装不成功
2.nginx配置
#设置低权限用户,为了安全而设置的
#user nobody;
#工作衍生进程数
worker_processes 10;
#设置错误文件存放路径
# error_log logs/error.log;
# error_log logs/error.log notice;
# error_log logs/error.log info;
#设置pid存放路径(pid是控制系统中重要文件)
pid logs/nginx.pid;
#设置最大连接数
events {
worker_connections 102400;
}
{
#文件扩展名与文件类型映射表
include mime.types;
map $time_iso8601 $logdate {
'~^(?\d{4}-\d{2}-\d{2})' $ymd;
default 'date-not-found';
}
#默认文件类型
default_type application/octet-stream;
limit_req_zone $binary_remote_addr zone=qps1:1m rate=800r/s;
#用来设置日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$'
'"$"$'
'"$upstream_addr" "$request_time" "$upstream_response_time"';
access_log off;
#用来指定日志文件的存放路径
# access_log logs/access-$logdate.log;
# error_log logs/error-$logdate.log;
#开启高效文件传输模式
sendfile on;
#防止网络阻塞
tcp_nopush on;
underscores_in_headers on;
#长连接超时时间,单位是秒
keepalive_timeout 120;
#(配置请求体缓存区大小)
client_max_body_size 50M;
#(设置客户端请求体最大值)
#client_body_buffer_size 128k;
#fastcgi_intercept_errors on;
#开启gzip压缩,开启后,访问网页会自动压缩
#gzip on;
#主要是用于设置一组可以在proxy_pass和fastcgi_pass指令中使用额代理服务器,默认负载均衡方式为轮询
upstream mysvr {
#设置同一个cookie的两次/多次请求,请求的是同一台服务器
#ip_hash;
#1.down 表示单前的server暂时不参与负载
#server 192.168.1.116 down;
#3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#server 192.168.2.21 backup;
#weigth参数表示权值,权值越高被分配到的几率越大,weight 默认为1.weight越大,负载的权重就越大。
#max_fails 配置的是熔断机制失败次数,即当访问一个服务器的时候,出现三次失败,就发生熔断
#fail_timeout 熔断时间,当服务器熔断超过10s后,nginx会重新尝试连接熔断的服务器
server 192.168.2.21:7798 weight=3 max_fails=6 fail_timeout=60; # 服务1
server 192.168.2.21:7798 weight=2 max_fails=6 fail_timeout=60; # 服务2
server 192.168.2.22:7798 weight=2 max_fails=6 fail_timeout=60; # 服务3
}
#指定服务器的名称和参数
server {
listen 80;
server_name aaa.com;
#proxy_connect_timeout 600;
#proxy_read_timeout 600;
#proxy_send_timeout 600;
#设置字符
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location /ams.cloud/report/getError.do {
return 200 '{code: "-1", message: "系统错误",success : false}';
}
#location / 指用根目录做负载均衡
location / {
#设置代理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8898;
#proxy_buffering off;
}
location /ams.api {
#set $version $http_version;
# 强制版本升级
#if ( $version != "2.0" ) {
# return 200 '{code: "-1", message: "版本过低请升级",success : false}';
#}
limit_req zone=qps1 burst=6000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mysvr;
}
location /CLodopfuncs.js {
proxy_pass http://localhost:8000/CLodopfuncs.js;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
二、服务器1、2、3配置(以一台讲解)
1.jdk安装与配置
2.tomcat
1.tomcat安装
2.tomcat配置
1.1 server.xml
1.2 web.xml
default
org.apache.catalina.servlets.DefaultServlet
debug
0
listings
true
1
jsp
org.apache.jasper.servlet.JspServlet
fork
false
xpoweredBy
false
3
default
/
jsp
*.jsp
*.jspx
30
index.html
index.htm
index.jsp
随心所往,看见未来。Follow your heart,see night!
欢迎点赞、关注、留言,一起学习、交流!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。