能够使用Nginx搭建Tomcat集群,并完成负载均衡
本文是基于mac系统设置;其他系统的基本类似
完成Nginx负载均衡,那么需要先来介绍Tomcat的安装和配置,我们首先要来配置Tomcat完成集群的配置.因为我们没有多台服务器运行Tomcat.那么我们可以模拟在一台服务器上运行多个Tomcat程序.
下载地址http://tomcat.apache.org/download-70.cgi
mac 环境下直接brew install Nginx 安装即可下载tomcat 拷贝两份;tomcat-8081 和 tomcat-8088 两个;分别修改conf/server.xml文件;修改内容是
<Server port="8025" shutdown="SHUTDOWN">//tomcat服务的地址的port
<Connector port="8088" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />//http 协议访问的地址的port
<Connector port="8029" protocol="AJP/1.3" redirectPort="8443" />//修改AJP访问地址的port
<Engine name="Catalina" defaultHost="localhost" jvmRoute=“tomcat-8088”>//引擎修改jvmRoute
</Server>
tomcat 修改完事
下面修改nginx
//usr/local/etc/nginx
修改nginx.conf 文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream server_lb{
server 127.0.0.1:8088 weight=1;
server 127.0.0.1:8081 weight=5;
}
//weight 是服务器的使用权重
server {
listen 8080;//nginx 默认的监听端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://server_lb;//反向代理服务器别名;与上述的一致
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;
}
# 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 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
好了先启动两个tomcat 然后
sudo nginx 启动nginx sudo php-fpm
修改 nginx.conf 后,重载配置文件
sudo nginx -s reload
停止 nginx 服务器
sudo nginx -s stop