2014-02-10 15:54:01
来 源
kejihao
Nginx
本文介绍Nginx+tomcat服务器环境负载均衡配置,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。

NGINX_TOMCAT 负载均衡配置

配置已在生产环境中运行。

软件:

Nginx-1.0.8

下载地址:http://nginx.org/download/nginx-1.0.8.tar.gz

apache-tomcat-6.0.18

下载地址:http://archive.apache.org/dist/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz

jdk-6u27-linux-i586.bin

下载地址:http://download.oracle.com/auth/otn-pub/java/jdk/6u27-b07/jdk-6u27-linux-i586.bin?e=131891933

Pcre-8.00

下载地址:http://nchc.dl.sourceforge.net/project/pcre/pcre/8.00/pcre-8.00.tar.gz

Nginx-upstream-jvm-route-0.2

下载地址:http://nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.2.tar.gz

配置环境:

Nginx服务器地址:192.168.1.20

Apache_tomcat1服务器:192.168.1.21

Apache_tomcat2服务器:192.168.1.22

一:软件安装步骤

将所有软件包下载到:/usr/src路径

配置nginx 服务器:

1:pcre-8.00编译安装

[[email protected] ~]# cd /usr/src/

[[email protected] src]# tar xf pcre-8.00.tar.gz

[[email protected] src]# cd pcre-8.00/

[[email protected] pcre-8.00]# ./configure ;make;make install

2:nginx-1.0.8编译安装

[[email protected] src]# tar xf nginx-upstream-jvm-route-0.1.tar.gz

[[email protected] src]# tar xf nginx-1.0.8.tar.gz

[[email protected] src]# cd nginx-1.0.8/

[[email protected] nginx-1.0.8]# patch -p0 < /usr/src/nginx_upstream_jvm_route/jvm_route.patch

[[email protected] nginx-1.0.8]# ./configure --prefix=/usr/local/nginx --with-pcre=/usr/src/pcre-8.00/ --with-http_stub_status_module --with-http_sub_module --add-module=/usr/src/nginx_upstream_jvm_route

3:修改Nginx 配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/

[[email protected] conf]# cp nginx.conf nginx.conf.back

[[email protected] conf]# vi nginx.conf

user nginx nginx;

worker_processes 8;

error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

pid        logs/nginx.pid;

  worker_rlimit_nofile 65535;

events {

use epoll;

worker_connections 10240;

}

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;

sendfile        on;

tcp_nopush     on;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

#keepalive_timeout 0;

keepalive_timeout 60;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_types   text/plain application/x-javascript text/css application/xml;

gzip_vary on;

server_tokens off;

upstream   backend 

{   

server 192.168.1.21:8080 srun_id=tomcat1; 

server 192.168.1.22:8080 srun_id=tomcat2; 

jvm_route $cookie_JSESSIONID|sessionid reverse;

}

server {

listen       192.168.1.20:80;

server_name web.cl.com;

charset utf-8;

#access_log logs/host.access.log main;

        location / {

root   html;

index index.html index.htm index.jsp;

proxy_redirect off;

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://backend;

}

location /status {   

stub_status   on;

access_log   on; 

auth_basic   "status"; 

auth_basic_user_file   /usr/local/nginx/htpasswd; 

}

#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;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ .php$ {

#    proxy_pass   http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ .php$ {

#    root           html;

#    fastcgi_pass   127.0.0.1:9000;

#    fastcgi_index index.php;

#    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

#    include        fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /.ht {

#    deny all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#    listen       8000;

#    listen       somename:8080;

#    server_name somename alias another.alias;

#    location / {

#        root   html;

#        index index.html index.htm;

#    }

#}

# HTTPS server

#

#server {

#    listen       443;

#    server_name localhost;

#    ssl                  on;

#    ssl_certificate      cert.pem;

#    ssl_certificate_key cert.key;

#    ssl_session_timeout 5m;

#    ssl_protocols SSLv2 SSLv3 TLSv1;

#    ssl_ciphers HIGH:!aNULL:!MD5;

#    ssl_prefer_server_ciphers   on;

#    location / {

#        root   html;

#        index index.html index.htm;

#    }

log_format   access   '$remote_addr - $remote_user [$time_local] "$request" ' 

'$status $body_bytes_sent "$http_referer" ' 

'"$http_user_agent" $http_x_forwarded_for'; 

access_log   /var/log/access.log   access;  

#}

}

添加用户:

[[email protected] conf]# groupadd nginx

[[email protected] conf]# useradd -g nginx -s /sbin/nologin nginx

添加查看Nginx服务状态的用户:

[[email protected] ~]# yum -y install httpd       安装此服务需要借助一个命令

[[email protected] ~]# htpasswd -c /usr/local/nginx/htpasswd nginx

New password:

Re-type new password:

检查配置文件:

[[email protected] conf]# /usr/local/nginx/sbin/nginx -t

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

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

看到以上两个结果就算配置是正确的。

启动服务:

[[email protected] conf]# /usr/local/nginx/sbin/nginx

停止服务:

[[email protected] conf]# /usr/local/nginx/sbin/nginx –s stop

也可以使用这个启动和停止的脚本:

[[email protected] conf]# vi /etc/init.d/nginx

#!/bin/sh  

#  

# description: Starts, stops nginx  

#  

#chkconfig: 2345 20 80 

#dscription: Startup script for nginx webserver on CentOS. Place in /etc/init.d   

#  

# Author: Touya  

set -e  

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/nginx  

DESC="nginx daemon" 

NAME=nginx  

DAEMON=/usr/local/nginx/sbin/$NAME   (定义nginx的安装目录)

CONFIGFILE=/usr/local/nginx/conf/nginx.conf  

PIDFILE=/usr/local/nginx/logs/$NAME.pid  

SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.  

test -x $DAEMON || exit 0 

d_start() {  

echo "Starting $DESC: $NAME" 

$DAEMON -c $CONFIGFILE || echo "already running" 

}  

d_stop() {  

echo "Stopping $DESC: $NAME" 

test -f $PIDFILE && kill -QUIT `cat $PIDFILE`  

}  

d_reload() {  

echo "Reloading $DESC configuration…" 

kill -HUP `cat $PIDFILE` || echo "can’t reload" 

}  

case "$1" in  

'start')  

d_start  

echo "started." 

;;  

'stop')  

d_stop  

echo "stoped." 

;;  

'reload')  

d_reload  

echo "reloaded." 

;;  

'restart')  

echo "Restarting $DESC: $NAME ..." 

d_stop  

# One second might not be time enough for a daemon to stop,  

# if this happens, d_start will fail (and dpkg will break if  

# the package is being upgraded). Change the timeout if needed  

# be, or change d_stop to have start-stop-daemon use --retry.  

# Notice that using --retry slows down the shutdown process somewhat.  

sleep 3 

d_start  

echo "done." 

;;  

'list')  

ps auxf | egrep '(PID|nginx)' | grep -v grep  

;;  

'test')  

$DAEMON -t -c $CONFIGFILE  

;;  

*)  

echo "Usage: $SCRIPTNAME {reload|list|test|start|stop|restart}" >&2 

exit 3 

;;  

esac  

exit 0 

[[email protected] conf]# chmod 755 /etc/init.d/nginx

[[email protected] conf]# /etc/init.d/nginx start

[[email protected] conf]# /etc/init.d/nginx stop

[[email protected] conf]# chkconfig --add nginx      #开机自启动

检查服务器端口是否启动:

[[email protected] conf]# lsof –i:80 

查看服务状态:

http://192.168.1.20/nginx

Apache-tomcat服务器配置

1:jdk-6u27-linux-i586.bin安装

[[email protected] usr]# cd /usr/src/

[[email protected] src]# ./jdk-6u27-linux-i586.bin

[[email protected] src]# mv jdk1.6.0_27/ /usr/local/

[[email protected] src]# vi /etc/profile

JAVA_HOME=/usr/local/ jdk1.6.0_27

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

PATH=$JAVA_HOME/bin:$PATH

TOMCAT_HOME=/usr/local/tomcat

export JAVA_HOME CLASSPATH PATH TOMCAT_HOME

使配置生效:

[[email protected] src]# source /etc/profile

查看当前java版本号:

[[email protected] src]# java -version

java version "1.6.0_27"

2:apache-tomcat-6.0.18安装

[[email protected] src]# tar xf apache-tomcat-6.0.18.tar.gz

[[email protected] src]# mv apache-tomcat-6.0.18 /usr/local/tomcat1

[[email protected] tomcat]# cd /usr/local/tomcat/conf/

修改配置文件:

[[email protected] conf]# vi server.xml

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

<Connector port="8009" maxThreads="500" minSpareThreads="30" maxSpareThreads="70" acceptCount="100" connectionTimeout="60000" protocol="AJP/1.3" redirectPort="8443" />

[[email protected] conf]# vi tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

<role rolename="manager"/>

<role rolename="admin"/>

<user username="admin" password="admin" roles="admin,manager"/>

</tomcat-users>

[[email protected] conf]# cd /usr/local/tomcat/bin/

[[email protected] bin]# vi catalina.sh

74  JAVA_HOME=/usr/local/ jdk1.6.0_27

JAVA_OPTS="-server -Xms256m -Xmx512m"

启动服务:

[[email protected] bin]# /usr/local/tomcat/bin/startup.sh

关闭服务:

[[email protected] bin]# /usr/local/tomcat/bin/shutdown.sh

检查服务端口:

[[email protected] bin]# lsof -i:8080

COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME

java    6414 root   29u IPv4 24953       TCP *:webcache (LISTEN)

在目录下建立测试页

[[email protected] ~]# mkdir /usr/local/tomcat/webapps/opps

[[email protected] ~]# vi /usr/local/tomcat/webapps/opps/mem.jsp

<%

Runtime lRuntime = Runtime.getRuntime();

out.println("*** BEGIN MEMORY STATISTICS ***<br/>");

out.println("Free Memory: "+lRuntime.freeMemory()+"<br/>");

out.println("Max   Memory: "+lRuntime.maxMemory()+"<br/>");

out.println("Total Memory: "+lRuntime.totalMemory()+"<br/>");

out.println("Available Processors : "+lRuntime.availableProcessors()+"<br/>");

out.println("*** END MEMORY STATISTICS ***");

%>

访问测试:

http://192.168.1.20/mem.jsp

能访问,证明配置成功。OK

在TOMCAT另一台服务器的配置步骤重复第一台的步骤就好了,在这里就不写出来了。

系统内核优化参数:(在三台上面添加)

[[email protected] bin]# /etc/sysctl.conf

# Use TCP syncookies when needed

net.ipv4.tcp_syncookies = 1

# Enable TCP window scaling

net.ipv4.tcp_window_scaling: = 1

# Increase TCP max buffer size

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

# Increase Linux autotuning TCP buffer limits

net.ipv4.tcp_rmem = 4096 87380 16777216

net.ipv4.tcp_wmem = 4096 65536 16777216

# Increase number of ports available

net.ipv4.ip_local_port_range = 1024 65000

使配置立即生效:

[[email protected] bin]# /sbin/sysctl -p

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。