本篇文章为你整理了ngnix+tomcat转发、负载均衡()的详细内容,包含有 ngnix+tomcat转发、负载均衡,希望能帮助你了解 ngnix+tomcat转发、负载均衡。
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;
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 "$http_referer"
"$http_user_agent" "$http_x_forwarded_for"
"$upstream_addr" "$request_time" "$upstream_response_time";
access_log off;
#用来指定日志文件的存放路径
# access_log logs/access-$logdate.log;
# error_log logs/error-$logdate.log;
#主要是用于设置一组可以在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
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;
Server port="8005" shutdown="SHUTDOWN"
Listener className="org.apache.catalina.startup.VersionLoggerListener" /
Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" /
Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /
Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /
Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" /
/GlobalNamingResources
port="8898" protocol="org.apache.coyote.http11.Http11AprProtocol"
connectionTimeout="5000"
minSpareThreads="20"
maxThreads="2048"
maxSpareThreads="2048"
acceptCount="2048"
minProcessors="20"
maxProcessors="2048"
enableLookups="false"
maxHttpHeaderSize ="102400"
maxPostSize="83886080"
redirectPort="8443" /
Realm className="org.apache.catalina.realm.LockOutRealm"
Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/
/Realm
Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t quot;%r quot; %s %b" /
/Host
/Engine
/Service
/Server
1.2 web.xml
?xml version="1.0" encoding="UTF-8"?
web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
servlet
servlet-name default /servlet-name
servlet-class org.apache.catalina.servlets.DefaultServlet /servlet-class
init-param
param-name debug /param-name
param-value 0 /param-value
/init-param
init-param
param-name listings /param-name
param-value true /param-value
/init-param
load-on-startup 1 /load-on-startup
/servlet
servlet-name jsp /servlet-name
servlet-class org.apache.jasper.servlet.JspServlet /servlet-class
init-param
param-name fork /param-name
param-value false /param-value
/init-param
init-param
param-name xpoweredBy /param-name
param-value false /param-value
/init-param
load-on-startup 3 /load-on-startup
/servlet
随心所往,看见未来。Follow your heart,see night!
欢迎点赞、关注、留言,一起学习、交流!
以上就是ngnix+tomcat转发、负载均衡()的详细内容,想要了解更多 ngnix+tomcat转发、负载均衡的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。