首页 > 代码库 > Linux下配置Nginx + Tomcat负载均衡
Linux下配置Nginx + Tomcat负载均衡
- Nginx简介
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过四年多了。Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集、 示例配置文件和低系统资源的消耗而闻名了。目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;国内几个重要的视频分享网站也部署了Nginx,如六房间、酷6等。 新近发现Nginx 技术在国内日趋火热,越来越多的网站开始部署Nginx。
- JDK安装
[root@localhost export]# chmod 777 jdk-6u45-linux-x64.bin[root@localhost export]# ./jdk-6u45-linux-x64.bin[root@localhost export]# mv jdk-6u45 jdk[root@localhost export]# vi /etc/profileexport JAVA_HOME=/usr/local/jdk1.7.0export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jarexport PATH=$JAVA_HOME/bin:$PATH保存退出:wq使环境变量生效#source /etc/profile测试JDK[root@localhost export]# java -versionjava version "1.6.0_45"Java(TM) SE Runtime Environment (build 1.6.0_45-b06)Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
- TOMCAT的安装
[root@localhost tomcat]# tar -zxvf apache-tomcat-7.0.53.tar.gz[root@localhost tomcat]# mv apache-tomcat-7.0.53 tomcat#ev-tomcatexport TOMCAT_HOME=/export/servers/tomcatexport CATALINA_HOME=/export/servers/tomcatexport CATALINA_BASE=/export/servers/tomcat保存退出:wq使环境变量生效[root@localhost tomcat]# source /etc/profile
修改TOMCAT配置文件/export/servers/tomcat/conf/server.xml
将以下内容
<Host name="localhost" appBase="webapps"
.../>
修改为
<Host name="localhost" appBase="/export/data/web"
.../>
启动tomcat[root@localhost bin]# sh startup.shUsing CATALINA_BASE: /export/servers/tomcatUsing CATALINA_HOME: /export/servers/tomcatUsing CATALINA_TMPDIR: /export/servers/tomcat/tempUsing JRE_HOME: /export/jdkUsing CLASSPATH: /export/servers/tomcat/bin/bootstrap.jar:/export/servers/tomcat/bin/tomcat-juli.jarTomcat started.关闭tomcat[root@localhost bin]# sh shutdown.shUsing CATALINA_BASE: /export/servers/tomcatUsing CATALINA_HOME: /export/servers/tomcatUsing CATALINA_TMPDIR: /export/servers/tomcat/tempUsing JRE_HOME: /export/jdkUsing CLASSPATH: /export/servers/tomcat/bin/bootstrap.jar:/export/servers/tomcat/bin/tomcat-juli.jar
- 同一linux系统下安装多个TOMCAT
将TOMCAT安装在/export/servers/tomcat_2
设置环境变量
#vi /etc/profile
export TOMCAT_2_HOME=/export/servers/tomcat_2
export CATALINA_2_HOME=/export/servers/tomcat_2
export CATALINA_2_BASE=/export/servers/tomcat_2
在PATH后面添加:$CATALINA_2_HOME/lib
保存退出
:wq
使环境变量生效
#source /etc/profile
修改TOMCAT配置文件/export/servers/tomcat_2/conf/server.xml
将以下内容
<Server port="8005" shutdown="SHUTDOWN">...
<Connector port="8080" protocol="HTTP/1.1" ...
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Host name="localhost" appBase="webapps"
.../>
修改为
<Server port="9005" shutdown="SHUTDOWN">...
<Connector port="9090" protocol="HTTP/1.1" ...
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
<Host name="localhost" appBase="/usr/local/www/web"
.../>
修改TOMCAT配置启动文件/export/servers/tomcat_2/bin/startup.sh
在exec上添加以下内容
export JAVA_HOME=/usr/java/jdk1.7.0
export CLASSPATH=$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin
export CATALINA_HOME=$CATALINA_2_HOME
export CATALINA_BASE=$CATALINA_2_BASE
修改TOMCAT配置启动文件/export/servers/tomcat_2/bin/shutdown.sh
在exec上添加以下内容
export JAVA_HOME=/usr/java/jdk1.7.0
export CLASSPATH=$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin
export CATALINA_HOME=$CATALINA_2_HOME
export CATALINA_BASE=$CATALINA_2_BASE
启动TOMCAT
/export/servers/tomcat_2/bin/startup.sh
关闭TOMCAT
/export/servers/tomcat_2/bin/shutdown.sh
- NGINX的安装
检查安装nginx的依赖性,nginx的模块需要第三方的支持,检查是否安装下列库:
[root@localhost bin]# yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
添加用户和组
[root@localhost bin]# groupadd nginx
[root@localhost bin]# useradd -g nginx nginx
安装nginx
[root@localhost servers]#tar -zxvf nginx-1.7.7.tar.gz
[root@localhost servers]#mv nginx-1.7.7 ngnix
[root@localhost servers]#cd ngnix
[root@localhost ngnix]#./configure --user=nginx --group=nginx --prefix=/export/servers/nginx
[root@localhost ngnix]#make
[root@localhost ngnix]#make install
修改/export/servers/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /export/home/www/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /export/home/www/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
upstream backend_server {
server 127.0.0.1:8080 weight=1 max_fails=2fail_timeout=30s;
server 127.0.0.1:9090 weight=1 max_fails=2fail_timeout=30s;
ip_hash;
}
server
{
listen 80;
server_name localhost;
index index.html index.htm;
root /export/home/www/web/ROOT;
location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key$host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_headerX-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
#用于清除缓存,假设一个URL为http://192.168.168.106/test.txt,通过访问http://192.168.168.106/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_headerX-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log off;
}
}
创建缓存文件夹
[root@localhost ngnix]#mkdir /export/home/www/proxy_temp_dir
[root@localhost ngnix]#mkdir /export/home/www/proxy_cache_dir
启动NGINX
[root@localhost ngnix]#./export/servers/nginx/sbin/nginx
关闭NGINX
[root@localhost ngnix]#./export/servers/nginx/sbin/nginx -s stop
重启NGINX
[root@localhost ngnix]#./export/servers/nginx/sbin/nginx -s relo
Linux下配置Nginx + Tomcat负载均衡