【转载】nginx, tomcat 集群
转载地址:
http://pengranxiang.iteye.com/blog/1135909
目标:同一台Linux主机上 安装 Nginx 和 两个 Tomcat 的集群
1. 下载安装 Nginx
地址:http://nginx.org/download/nginx-1.0.4.tar.gz
Shell代码:
cd /usr/local/src/nginx
wget http://nginx.org/download/nginx-1.0.4.tar.gz
tar zxvf nginx-1.0.4.tar.gz
cd nginx-1.0.4
./configure
make
make install
2. Tomcat的下载安装配置,请参照 http://pengranxiang.iteye.com/admin/blogs/1135072
3. 配置 Nginx 负载均衡 来集成 两个Tomcat
修改配置文件:$NGNINX_HOME/conf/nginx.conf
Conf代码 :
#Nginx所用用户和组,window下不指定
user nobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes 1;
#错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放文件
pid logs/nginx.pid;
events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#允许最大连接数
worker_connections 1024;
}
http {
#mine.types内定义各文件类型映像
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#负载均衡集群设置
upstream tomcats {
server localhost:8080 weight=1;
server localhost:9080 weight=1;
#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同
ip_hash;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.shtml;
proxy_pass http://tomcats;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
安装以上配置即可实现集群。
关于Nginx启动
直接执行: $NGINX_HOME/sbin/nginx 即可
为了方便,可以自定义一个脚本文件
vim nginx.sh
Sh代码 :
#!/bin/bash
case $1 in
start)
/usr/local/nginx/sbin/nginx;
;;
stop)
kill -2 `ps -ef|grep "/usr/local/nginx/sbin/nginx" | grep -v "grep"|awk '{print $2}'`
;;
restart)
$0 stop
$0 start
;;
*)
echo $"Usage; $0{start|stop|restart}"
exit 1
esac
exit 0