Mac 环境下搭建 Nginx + Tomcat集群, Nginx + Tomcat集群是大家常用的一种搭配, 好处有很多, 而我做这个的初衷就2个目的, 1: 解决tomcat的复杂均衡问题, 2. 当我上线的时候, 启动tomcat, 能够做到外部访问不间断。
下面开始我们的步骤:
第一步: 安装我们的Nginx, Mac上是利用brew安装的;
[linuxidc@www.linuxidc.com:~]$ brew install nginx
==> Installing nginx dependency: pcre
==> Downloading http://downloads.sourceforge.net/project/pcre/pcre/8.34/pcre-8.3 ######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/pcre/8.34 --enable-utf8 --enable-unic
==> make
==> make test
==> make install /usr/local/Cellar/pcre/8.34: 140 files, 4.1M, built in 58 second>
==> Installing nginx
差不多就这一步, 我们看看是够安装成功:
[linuxidc@www.linuxidc.com:~]$ nginx -V
nginx version: nginx/1.4.4
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.4.4 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/usr/local/Cellar/nginx/1.4.4/bin/nginx --with-cc-opt=-I/usr/local/include --with-ld-opt=-L/usr/local/lib --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-http_gzip_static_module
能看见, 这里安装的是 1.4.4版本的.
顺便告诉大家, brew安装的文件路径一般默认在:
[linuxidc@www.linuxidc.com:Cellar]$ pwd
/usr/local/Cellar
然而, nginx的真正路径在:
[linuxidc@www.linuxidc.com:nginx]$ pwd
/usr/local/etc/nginx
我们启动nginx服务器:
[linuxidc@www.linuxidc.com:~]$ sudo nginx
Password:
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
nginx: [emerg] still could not bind()
输入你的个人密码, 就算是启动了:
这就算启动了. 很简单吧~
如果是查看nginx启动进程
[linuxidc@www.linuxidc.com:~]$ ps -ef|grep nginx
0 8220 1 0 9:07PM ?? 0:00.00 nginx: master process nginx
502 8322 8220 0 9:34PM ?? 0:00.87 nginx: worker process
502 8323 8220 0 9:34PM ?? 0:00.15 nginx: worker process
502 8704 8598 0 8:27PM ttys000 0:00.00 grep nginx
关闭nginx服务器:
[linuxidc@www.linuxidc.com:~]$ sudo nginx -s stop
============= 现在就差不多把nginx的安装说明白了. OK吗?
第二步: 我们在本机部署2台Tomcat容器, 并且要正常跑起来. 具体看看:
你需要下载一个Tomcat, 这个我觉得不需要在说怎么下载Tomcat了吧..我一般会去官方下载tar包.
apache-tomcat-7.0.37
下载之后, 我们将名字修改为:
drwxr-xr-x 16 linuxidc staff 544 Jun 21 18:39 tomcat-7-2
drwxr-xr-x 16 linuxidc staff 544 Jun 21 18:41 tomcat-7-3
我这里暂且从2开始取名吧~ 因为我本地还有一个tomcat-7-1, 自己开发用的. 反正这只是一个文件夹的名字而已, 应该不会造成混淆. 我们就以 tomcat-7-2, tomcat-7-3, 开举例子:
如果需要让2台Tomcat同时启动, 我们是需要修改配置文件的, 因为Tomcat绑定的端口号是唯一的, 其实我们就是让他们的端口号不冲突而已. 这样子就能跑起2台Tomcat了.
进入tomcat-7-2的配置
[linuxidc@www.linuxidc.com:conf]$ pwd
/Users/linuxidc/Library/tomcat-7-2/conf
[linuxidc@www.linuxidc.com:conf]$ ls -l
total 408
drwxr-xr-x 3 linuxidc staff 102 Jun 21 18:39 Catalina
-rw-r--r-- 1 linuxidc staff 12377 Jun 21 18:39 catalina.policy
-rw-r--r-- 1 linuxidc staff 6528 Jun 21 18:39 catalina.properties
-rw-r--r-- 1 linuxidc staff 1392 Jun 21 18:39 context.xml
-rw-r--r-- 1 linuxidc staff 3352 Jun 21 18:39 logging.properties
-rw-r--r-- 1 linuxidc staff 6455 Jun 21 18:52 server.xml
-rw-r--r-- 1 linuxidc staff 1528 Jun 21 18:39 tomcat-users.xml
-rw-r--r-- 1 linuxidc staff 162892 Jun 21 18:39 web.xml
[linuxidc@www.linuxidc.com:conf]$
我们打开server.xml文件, 这里就是我们需要配置的地方, 利用open命令打开, 很爽的一个命令, 我这里设置默认打开为 Sublime Text 2, 一般默认为 VIM.
[linuxidc@www.linuxidc.com:conf]$ open server.xml
[linuxidc@www.linuxidc.com:conf]$
<Server port="8105" shutdown="SHUTDOWN">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8180" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8109" protocol="AJP/1.3" redirectPort="8443"/>
这3个关键子地方的port, 很好记, 我现在修改后都是以 81开头的, 而之后的tomcat-7-3, 我就会以82开头:
<Server port="8205" shutdown="SHUTDOWN">
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8280" protocol="HTTP/1.1" redirectPort="8443"/>
<Connector port="8209" protocol="AJP/1.3" redirectPort="8443"/>
这里就算配置完了, 我们可以建立明目直接发布到每个不同的tomcat中, 也可以直接跑. 我这类是直接发布的我自己的一个war包. ROOT.war 到 webapp:
[linuxidc@www.linuxidc.com:tomcat-7-2]$ cd webapps/
[linuxidc@www.linuxidc.com:webapps]$ ls -l
total 23352
drwxr-xr-x 10 linuxidc staff 340 Jun 21 19:03 ROOT
-rw-r--r-- 1 linuxidc staff 11953340 Jun 21 19:00 ROOT.war
drwxr-xr-x 53 linuxidc staff 1802 Jun 21 18:39 docs
drwxr-xr-x 7 linuxidc staff 238 Jun 21 18:39 examples
drwxr-xr-x 7 linuxidc staff 238 Jun 21 18:39 host-manager
drwxr-xr-x 8 linuxidc staff 272 Jun 21 18:39 manager
[linuxidc@www.linuxidc.com:webapps]$
我们分开启动2个Tomcat: 这个在tomcat的bin目录下.
[linuxidc@www.linuxidc.com:bin]$ sh startup.sh
OK, 我们访问: 我直接上图:
这就说明已经成功启动了2台:
第三步: 修改nginx配置, 让其做好tomcat的跳转:
进入:
[linuxidc@www.linuxidc.com:nginx]$ pwd
/usr/local/etc/nginx
打开其中的 nginx.conf 文件: 我将贴上最基本的全文件供大家参考:
user linuxidc staff ;
worker_processes 2;
#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 localhost {
#ip_hash;
server localhost:8180;
server localhost:8280;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect HOST default;
}
#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;
# }
#}
#include /usr/local/etc/nginx/conf.d/*.conf;
}
其中需要注意的地方:
upstream localhost {
#ip_hash;
server localhost:8180;
server localhost:8280;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect HOST default;
}
然后重启nginx服务器: 或者关闭后开启动都一样:
最后我们输入 http://localhost
第四步: 测试是够真的跳转了不同的服务器:
我们修改tomcat-7-2中的一个JSP页面, 而tomcat-7-3不要修改. 我们不停的刷新, 我们可以看见一会儿有变化, 一会儿不会变化, 这样说明OK了.
我们停掉tomcat-7-2, 依然能够访问;
我们停掉tomcat-7-3, 依然能够访问;
这样子就算搭建好了 Nginx + Tomcat7.0 集群. 不算很难吧~ 我也是新手, 忘大家拍砖...
(责任编辑:IT) |