Nginx 是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。Nginx、apache是基于http反向代理方式,Nginx不能为基于TCP协议的应用提供负载均衡。 其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。javascript
安装软件:css
安装nginx、tomcat等软件:安装方式可自行选择html
确保nginx、tomcat均可成功访问java
此处使用upstream hash来完成不一样后端服务器之间的无状态切换:linux
安装配置详见:http://wiki.nginx.org/HttpUpstreamRequestHashModule nginx
tomcat配置:web
为了使一台机器同时启动两台tomcat,须要对tomcat配置文件作以下的修改:apache
修改tomcat1 的配置:apache-tomcat-8.0.23/conf/server.xml,修改三处端口,把默认的端口修改成一个新的未使用的端口后端
<!-- 修改port端口:18006 俩个tomcat不能重复,随意设置一个未使用的端口-->
<Server port="18006" shutdown="SHUTDOWN">
<!-- port="18081" tomcat监听端口,随意设置一个未使用的端口 -->
<Connector port="18081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> tomcat
nginx配置
nginx 配置文件设置nginx.conf:
如下标红的为必须配置的:
在http配置中配置
#Nginx所用用户和组,window下不指定
#user niumd niumd;
#工做的子进程数量(一般等于CPU数量或者2倍于CPU)
worker_processes 2;
#错误日志存放路径
#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 2048;
}
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"';
log_format main '$remote_addr\t$http_x_forwarded_for\t-\t$remote_user\t$time_iso8601\t$request_method\t"$document_uri"\t"$query_string"\t$server_protocol\t$status\t$body_bytes_sent\t$request_time\t"$http_referer"\t"$http_user_agent"\t$http_Cdn-Src-Ip\t$host\t$hostname\t$server_addr\t-\t-\t-\t-\t-\tV5';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 15;
concat on;
#gzip on;
upstream 10.27.171.57 { #两个tomcat负载均衡配置,部署在同一个机器上不一样的端口
hash $cookie_jsessionid;
server 10.27.171.57:8090;
server 10.27.171.57:8080;
hash_again 1;
}
gzip on;
gzip_vary on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml application/javascript text/javascript;
gzip_http_version 1.1;
proxy_buffering off;
server {
listen 80; #监听80端口,默认直接使用ip便可访问
#server_name localhost;
server_name 10.27.171.57; #访问的server名称
#charset koi8-r;
access_log /opt/rsync_log/access_http.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://10.27.171.57; #须要访问的server地址
}
}
}
验证集群部署是否成功,在这里停掉tomcat1,而后访问http://10.27.171.57