693579551 发表于 2016-12-23 06:32:14

【转载】nginx, tomcat 集群

【转载】nginx, tomcat 集群
转载地址:http://pengranxiang.iyunv.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.iyunv.com/admin/blogs/1135072

3. 配置 Nginx 负载均衡 来集成 两个Tomcat

修改配置文件:$NGNINX_HOME/conf/nginx.conf

Conf代码 :
    #Nginx所用用户和组,window下不指定
usernobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes1;
#错误日志存放路径
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
#指定pid存放文件
pid      logs/nginx.pid;

events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#允许最大连接数   
worker_connections1024;
}

http {
#mine.types内定义各文件类型映像
include       mime.types;
default_typeapplication/octet-stream;
#定义日志格式
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
#tcp_nopush   on;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
#负载均衡集群设置
upstream tomcats {
server localhost:8080 weight=1;
server localhost:9080 weight=1;
#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同
ip_hash;
}   
server {
listen       80;
server_namelocalhost;
#charset koi8-r;
#access_loglogs/host.access.logmain;
location / {
indexindex.shtml;
proxy_passhttp://tomcats;
proxy_set_header    X-Real-IP   $remote_addr;
client_max_body_size    100m;
}
#error_page404            /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_indexindex.php;
#    fastcgi_paramSCRIPT_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 {
#    denyall;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_namesomenamealiasanother.alias;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443;
#    server_namelocalhost;
#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_keycert.key;
#    ssl_session_timeout5m;
#    ssl_protocolsSSLv2 SSLv3 TLSv1;
#    ssl_ciphersALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#    ssl_prefer_server_ciphers   on;
#    location / {
#      root   html;
#      indexindex.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
页: [1]
查看完整版本: 【转载】nginx, tomcat 集群