nginx配置负载

时间:2022-09-17 12:07:47

一.系统优化

1、修改/etc/sysctl.conf,优化tcp连接数

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

2、修改/etc/security/limits.conf,修改最大文件打开数为65535

添加如下两行:

*      soft  nofile       65535

*      hard nofile       65535

*      soft  nproc       16384

*      hard nproc       16384

二.安装JDK

chmod +x jdk-6u38-linux-x64.bin

# ./jdk-6u38-linux-x64.bin

#添加环境变量

# vi /etc/profile

export JAVA_HOME=/kjt/java/jdk1.6.0_38

export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

#使环境变量生效

# source /etc/profile

#查看java版本

# java -version

java version "1.6.0_38"

Java(TM) SE Runtime Environment (build 1.6.0_27-b07)

Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

三.安装Tomcat

1、确保防火墙已关闭

#service iptables stop

#chkconfig iptables off

#vi /etc/selinux/config

selinux=disabled

2、安装tomcat

# tar zxvf apache-tomcat-6.0.36.tar.gz

# cd apache-tomcat-6.0.36

#修改/bin/catalina.sh,在cygwin=false前加路径

export JAVA_HOME=/kjt/java/jdk1.6.0_38/

export PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export TOMCAT_HOME=/kjt/java/apache-tomcat-6.0.36

export CATALINA_HOME=/kjt/java/apache-tomcat-6.0.36

export CATALINA_BASE=/kjt/java/apache-tomcat-6.0.36

3、修改/conf/server.xml,做tomcat线程池优化

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="15000" maxThreads="500" minSpareThreads="50" maxSpareTheads="250" maxProcessors="600"

acceptCount="1000" enableLookups="false" compression="on"

redirectPort="8443" URIEncoding="utf-8" useBodyEncodingForURI="true"/>

<!-- A "Connector" using the shared thread pool-->

4、启动tomcat

cd /kjt/java/apache-tomcat-6.0.35/bin

./startup

5、查看tomcat进程

ps –ef|grep tomcat

四.Nginx配置

1.确定Nginx所需pcre库、openssl库等依赖包已经安装,若没有安装,安装nginx过程中将报错。

2.安装Nginx

tar zxvf nginx-1.0.15.tar.gz

cd nginx-1.0.15/

./configure  --prefix=/kjt/app/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

3.Nginx配置文件如下:

nginx配置负载

4.测试配置文件

/kjt/app/nginx/sbin/nginx -t

显示以下信息为正确的

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful

5、启动nginx服务

/kjt/app/nginx/sbin/nginx

6、查看nginx进程

ps -ef|grep nginx

五.安装配置Keepalived

1、安装keepalived

tar -zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/kjt/app/keepalived

make

make install

2、将keepalived设置为开机启动

cp /kjt/app/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp/kjt/app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /kjt/app/keepalived/sbin/keepalived /usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

3.修改配置文件为以下内容:

mkdir /etc/keepalived

cp /kjt/app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

vim  /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_1

}

vrrp_script Monitor_Nginx {

script "/kjt/app/keepalived/etc/keepalived/samples/monitor_nginx.sh
"  #脚本存放位置

interval 2     #检查间隔为2秒

weight 2

}

vrrp_instance VI_1 {

state
BACKUP  #设置nopreempt,表示之前挂掉的master重新恢复后不再切换为master,直接成为BACKUP,nopreempt只能配置在BACKUP状态下

nopreempt

interface eth0

virtual_router_id 51

priority 100    #优先级较高的成为MASTER

advert_int 5   #健康检查间隔为5秒

authentication {

auth_type PASS

auth_pass kjt2013

}

virtual_ipaddress {

10.46.86.60

}

track_script {

Monitor_Nginx

}

}

注:monitor_nginx.sh为监控nginx进程的脚本,内容如下:当检测到nginx主进程挂掉之后,系统将自动重启nginx,若nginx主进程重启失败,延迟5秒后杀掉keepalived进程,进行切换。

#!/bin/bash

if [ "$(ps -ef | grep "nginx:
master process"| grep -v grep )" == "" ]

then

/kjt/app/nginx/sbin/nginx

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v
grep )" == "" ]

then

killall keepalived

fi

fi

5.启动keepalived

service keepalived start

注:备机的keepalived的安装和上面一样,只要把配置文件中的priority改为小于100的数即可

6、配置Keepalived监控脚本,每分钟执行一次,当检测到原先MASTER上的Keepalived进程挂掉之后,将重启Keepalived进程

nginx配置负载

vi  /root/shell/keepalived.sh

touch  /root/shell/keepalived.log

chmod 755 *

新增计划任务

crontab –e

* * * * * (sh /root/shell/keepalived.sh
>> /root/shell/keepalived.log)

六.测试步骤

1.访问VIP看是否能够正常访问后端的tomcat

2.停止其中一个tomcat看是否能将访问转到另一台上

3.停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

VRRP_Script(Monitor_Nginx) timed out

VRRP_Script(Monitor_Nginx) succeeded

4.停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

tail 
-f  /var/log/messages

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

MASTER机器上日志会显示

Keepalived_vrrp: Terminating VRRP child
process on signal

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

现在把原先MASTER上的Keepalived重新启动,会看到原先MASTER直接成为BACKUP,不再做一次切换。

现在的BACKUP日志如下:

Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

Master日志如下:

Keepalived_vrrp: VRRP_Script(Monitor_Nginx)
succeeded

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

一.系统优化

1、修改/etc/sysctl.conf,优化tcp连接数

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

2、修改/etc/security/limits.conf,修改最大文件打开数为65535

添加如下两行:

*      soft  nofile       65535

*      hard nofile       65535

*      soft  nproc       16384

*      hard nproc       16384

二.安装JDK

chmod +x jdk-6u38-linux-x64.bin

# ./jdk-6u38-linux-x64.bin

#添加环境变量

# vi /etc/profile

export JAVA_HOME=/kjt/java/jdk1.6.0_38

export
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

export PATH=$JAVA_HOME/bin:$PATH

#使环境变量生效

# source /etc/profile

#查看java版本

# java -version

java version "1.6.0_38"

Java(TM) SE Runtime Environment (build
1.6.0_27-b07)

Java HotSpot(TM) 64-Bit Server VM (build
20.2-b06, mixed mode)

三.安装Tomcat

1、确保防火墙已关闭

#service iptables stop

#chkconfig iptables off

#vi /etc/selinux/config

selinux=disabled

2、安装tomcat

# tar zxvf apache-tomcat-6.0.36.tar.gz

# cd apache-tomcat-6.0.36

#修改/bin/catalina.sh,在cygwin=false前加路径

export JAVA_HOME=/kjt/java/jdk1.6.0_38/

export PATH=$JAVA_HOME/bin:$PATH

export
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export
TOMCAT_HOME=/kjt/java/apache-tomcat-6.0.36

export
CATALINA_HOME=/kjt/java/apache-tomcat-6.0.36

export
CATALINA_BASE=/kjt/java/apache-tomcat-6.0.36

3、修改/conf/server.xml,做tomcat线程池优化

<Connector port="8081" protocol="HTTP/1.1"

connectionTimeout="15000" maxThreads="500"
minSpareThreads="50" maxSpareTheads="250"
maxProcessors="600"

acceptCount="1000"
enableLookups="false" compression="on"

redirectPort="8443"
URIEncoding="utf-8" useBodyEncodingForURI="true"/>

<!-- A
"Connector" using the shared thread pool-->

4、启动tomcat

cd /kjt/java/apache-tomcat-6.0.35/bin

./startup

5、查看tomcat进程

ps –ef|grep tomcat

四.Nginx配置

1.确定Nginx所需pcre库、openssl库等依赖包已经安装,若没有安装,安装nginx过程中将报错。

2.安装Nginx

tar zxvf nginx-1.0.15.tar.gz

cd nginx-1.0.15/

./configure  --prefix=/kjt/app/nginx --with-http_stub_status_module
--with-http_ssl_module

make

make install

3.Nginx配置文件如下:

nginx配置负载

4.测试配置文件

/kjt/app/nginx/sbin/nginx -t

显示以下信息为正确的

the configuration file
/usr/local/nginx/conf/nginx.conf syntax is ok

configuration file
/usr/local/nginx/conf/nginx.conf test is successful

5、启动nginx服务

/kjt/app/nginx/sbin/nginx

6、查看nginx进程

ps -ef|grep nginx

五.安装配置Keepalived

1、安装keepalived

tar -zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/kjt/app/keepalived

make

make install

2、将keepalived设置为开机启动

cp /kjt/app/keepalived/etc/rc.d/init.d/keepalived
/etc/rc.d/init.d/

cp/kjt/app/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/

cp /kjt/app/keepalived/sbin/keepalived
/usr/sbin/

chkconfig --add keepalived

chkconfig keepalived on

3.修改配置文件为以下内容:

mkdir /etc/keepalived

cp /kjt/app/keepalived/etc/keepalived/keepalived.conf
/etc/keepalived/

vim  /etc/keepalived/keepalived.conf

nginx配置负载

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_1

}

vrrp_script Monitor_Nginx {

script "/kjt/app/keepalived/etc/keepalived/samples/monitor_nginx.sh
"  #脚本存放位置

interval 2     #检查间隔为2秒

weight 2

}

vrrp_instance VI_1 {

state
BACKUP  #设置nopreempt,表示之前挂掉的master重新恢复后不再切换为master,直接成为BACKUP,nopreempt只能配置在BACKUP状态下

nopreempt

interface eth0

virtual_router_id 51

priority 100    #优先级较高的成为MASTER

advert_int 5   #健康检查间隔为5秒

authentication {

auth_type PASS

auth_pass kjt2013

}

virtual_ipaddress {

10.46.86.60

}

track_script {

Monitor_Nginx

}

}

注:monitor_nginx.sh为监控nginx进程的脚本,内容如下:当检测到nginx主进程挂掉之后,系统将自动重启nginx,若nginx主进程重启失败,延迟5秒后杀掉keepalived进程,进行切换。

nginx配置负载

#!/bin/bash

if [ "$(ps -ef | grep "nginx:
master process"| grep -v grep )" == "" ]

then

/kjt/app/nginx/sbin/nginx

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v
grep )" == "" ]

then

killall keepalived

fi

fi

5.启动keepalived

service keepalived start

注:备机的keepalived的安装和上面一样,只要把配置文件中的priority改为小于100的数即可

6、配置Keepalived监控脚本,每分钟执行一次,当检测到原先MASTER上的Keepalived进程挂掉之后,将重启Keepalived进程

nginx配置负载

vi  /root/shell/keepalived.sh

touch  /root/shell/keepalived.log

chmod 755 *

新增计划任务

crontab –e

* * * * * (sh /root/shell/keepalived.sh
>> /root/shell/keepalived.log)

六.测试步骤

1.访问VIP看是否能够正常访问后端的tomcat

2.停止其中一个tomcat看是否能将访问转到另一台上

3.停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

VRRP_Script(Monitor_Nginx) timed out

VRRP_Script(Monitor_Nginx) succeeded

4.停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

tail 
-f  /var/log/messages

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

MASTER机器上日志会显示

Keepalived_vrrp: Terminating VRRP child
process on signal

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

现在把原先MASTER上的Keepalived重新启动,会看到原先MASTER直接成为BACKUP,不再做一次切换。

现在的BACKUP日志如下:

Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

Master日志如下:

Keepalived_vrrp: VRRP_Script(Monitor_Nginx)
succeeded

Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.46.86.60

nginx配置负载的更多相关文章

  1. 通过http、https域名访问静态网页、nginx配置负载均衡(nginx配置)

    很多场景下需要可以通过浏览器访问静态网页,不想把服务器ip地址直接暴露出来,通过nginx可以解决这个问题. 实现http域名访问静态网页 1.域名解析配置(本文都是以阿里云为例,其他平台,操作步骤类 ...

  2. 解决nginx配置负载均衡时invalid host in upstream报错

    当前平台: windows nginx版本: 1.11.5 前言: 在配置负载均衡时,同时也需要设置反向代理,当修改了nginx.conf时,发现nginx服务无法开启. 1. 打开"ngi ...

  3. nginx配置 负载均衡

    配置nginx #配置虚拟主机 server { listen 80; server_name www.testaaa.com; location / { #root /usr/local/nginx ...

  4. NginX——配置负载均衡

    A.            在http模块加上upstream配置 upstream www.myweb.com { server  127.0.0.1:9100 weight=3; server  ...

  5. 图解nginx配置负载均衡

    1. 在Linux上准备两份tomcat 2. 修改两份tomcat的端口号 修改的端口如图所示: 3. 启动两个tomcat服务器 4. 修改两个服务器上的主页方便测试区分 5. 在nginx配置文 ...

  6. nginx集群&colon;nginx配置负载均衡集群&lpar;nginx1&period;18&period;0&rpar;

    一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 nginx集群:功能更强:可 ...

  7. nginx配置负载均衡与反向代理

    #给文件夹授权   1 chown -R www:www /usr/local/nginx #修改配置文件vim nginx.conf   1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  8. nginx配置负载均衡

    本教程不讲解nginx的安装,若安装请看博客 http://www.cnblogs.com/hqjy/p/8092983.html 本教程不讲解tomcat的安装,若安装请看博客 http://www ...

  9. Nginx 配置负载均衡

    nginx负载均衡配置,主要是proxy_pass,upstream的使用. 注意问题,多台机器间session的共享问题. 不用session,用户cookie.或者用redis替代session. ...

随机推荐

  1. jQuery标准的AJAX模板

    $('#saveInformationTemplate_button').on('click', function(){ if(isEmpty($("#name").val())) ...

  2. sqlserver查询效率

    很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...

  3. Linux(Ubuntu)下面SecureCRT 完全破解

    转载声明:本文来自http://www.boll.me/archives/680 相关说明: 上篇发了个Linux(Ubuntu) 下 SecureCRT 7 30天循环破解在启动的时候会多输入一次确 ...

  4. 在js中如何得到上传文件的大小。

    <html>  <head>  <script language="javascript">    function getSize() {   ...

  5. Expedition---POJ - 2431

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...

  6. linux 将自己的服务添加到系统service服务

    前言 我们在linux上要启动一个程序得时候, 往往都是要写一堆路径, 找到要启动得服务程序, 再用 ./*** 启动服务. 那么我们有没有快速启动方法吗, 答案是肯定得 service 介绍 官方介 ...

  7. 500&period;19 ,错误:4&period;00x80070005

    直接把网站根目录添加上everyone权限即可

  8. express框架结合jade模板引擎使用

    在views文件夹里新建一个jade.jade文件作为模板: html head title 哈哈 body #box ul li 标题1 li 标题2 li 标题3 li 标题4 #aside 在j ...

  9. OpenCL 双调排序 CPU 版

    ▶ 学习了双调排序,参考(https://blog.csdn.net/xbinworld/article/details/76408595) ● 使用 CPU 排序的代码 #include <s ...

  10. webgl学习笔记三-平移旋转缩放

    写在前面 建议先阅读下前面我的两篇文章. webgl学习笔记一-绘图单点 webgl学习笔记二-绘图多点 平移 1.关键点说明 顶点着色器需要加上 uniform vec4 u_Translation ...