器量大者,福泽必厚
全部博文(587)
分类: LINUX
2015-12-01 15:47:03
测试环境为:
Nginx+2台tomcat负载均衡
地址信息如下:
Nginx: 10.33.96.251
Tomcat1:10.33.97.246
Tomcat2:10.33.97.247
搭建如下:
1:分别在246和247机器上搭建tomcat
先安装jdk,我使用的版本为jdk-7u79-linux-x64.tar.gz
到/home/目录下解压即可
tar zxvf jdk-7u79-linux-x64.tar.gz
ln -s jdk1.7.0_79 jdk
然后配置环境变量:
Vi /etc/profile ##加入如下3条记录
export JAVA_HOME=/home/jdk
export PATH=$GOROOT/bin:$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
source /etc/profile 使其立即生效!
2:安装tomcat
我下载的是apache-tomcat-7.0.62.tar.gz ,
tar zxvf apache-tomcat-7.0.62.tar.gz -C /usr/local
ln -s apache-tomcat-7.0.62 tomcat
3:测试tomcat是否能成功启动!
/usr/local/tomcat/bin/start.sh
lsof -i:8080 如果有类似如下的
输出,就表示tomcat已经启动!
4:将研发同事提供的war文件放到tomcat下的webapps目录下!
##我在这里遇到一个问题war文件不能解压,我解决的方法是jar xvf *.war 即可
##以上的步骤在两台tomcat机器上都要执行!
5:下面是优化tomcat的步骤:
5.1:安装apr、apr-util、tomcat-native
安装apr:
tar zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
make
make install
安装apr-util:
tar zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --with-apr=/usr/local/apr/bin/apr-1-config --prefix=/usr/local/apr-util
make
make install
安装tomcat-native:
cd /usr/local/tomcat/bin/
tar zxvf tomcat-native.tar.gz
cd /usr/local/tomcat/bin/tomcat-native-1.1.33-src/jni/native
./configure --with-apr=/usr/local/apr --with-java-home=/home/jdk
make
make install
然后在/usr/local/tomcat/bin/catalina.sh 中添加:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
export LD_LIBRARY_PATH
然后重启tomcat,如果重启过程中在日志文件中看到如下的字样:
就表示apr已经安装
5.2:vi /usr/local/tomcat/conf/catalina.properties
加入:
JAVA_OPTS=" -server -Xms2000M -Xmx2000M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:PermSize=64M -XX:MaxPermSize=300M -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=31 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -Djava.awt.headless=true"
##尤其要注意根据自己的配置修改,-Xms和-Xmx应该相同!
5.3:修改如下的配置
URIEncoding="UTF-8"
minSpareThreads="50"
maxSpareThreads="150"
enableLookups="false"
disableUploadTimeout="true"
connectionTimeout="20000"
acceptCount="300"
maxThreads="400"
useURIValidationHack="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
redirectPort="8443"/>
5.4:重启tomcat使上面的配置生效!
6:安装nginx
tar zxvf nginx-1.8.0_.tar.gz
cd nginx-1.8.0
./configure --user=nobody --group=nobody --prefix=/data/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module
make
make install
6:nginx配置文件:
root@ubuntu:/data/nginx/conf# cat nginx.conf
user nobody;
worker_processes 2;
worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 81920;
}
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;
server_tokens off;
sendfile on;
tcp_nopush on;
fastcgi_intercept_errors on;
keepalive_timeout 65;
gzip on;
gzip_comp_level 5;
gzip_http_version 1.0;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_types text/plain application/x-javascript text/css application/xml;
client_max_body_size 50M;
client_header_buffer_size 8k;
large_client_header_buffers 4 16k;
fastcgi_connect_timeout 1000;
fastcgi_send_timeout 1000;
fastcgi_read_timeout 1000;
fastcgi_buffers 32 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream nodesocket {
server 10.33.97.246:8080 fail_timeout=20s;
server 10.33.97.247:8080 fail_timeout=20s;
}
server {
listen 80;
server_name 10.33.96.251;
location / {
proxy_pass
#链接超时时间,就是nginx读取后端服务器数据的时间,对websocket也有效
proxy_read_timeout 1800;
#以下三行,是开启对websocket的支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
####这里有一个问题,这样访问的话,tomcat的后台日志不能记录客户端的真实ip,
后来在tomcat 下的server.xml中配置如下:
然后重启tomcat服务,这样就可以在日志文件里面有客户端的ip地址: