nginx与tomcat结合
Nginx与Tomcat整合的好处如下所示:
◆静态分离,加快用户访问网站的速度。
◆整个负载均衡层和Web层的工作流程为LVS/DR+Keeaplived→Nginx反向代理(动静分离)→Tomcat集群,可以保证整个网站不会因为某一台LVS或Nginx+tomcat机器挂掉而影响网站的运营。
◆Nginx稳定,宕机的可能性微乎其乎。
下面是实施的具体步骤
由于服务器均采用的是最小化安装,所以先安装一些基础的编译库,命令如下:
[root@localhost ~]# yum -y install gcc gcc-c++ autoconf libjpeglibjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-develzlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncursesncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidnlibidn-devel openssl openssl-devel
(1)首先分别在xxx.xxx.xxx.146和xxx.xxx.xxx.147上安装Nginx和Tomcat,然后整合它们。
在安装Tomcat 之前,必须先安装JDK(参考文档).
然后下载并安装apache-tomat,如下所示。
wget http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-7/v7.0.12/bin/apache-tomcat7.0.12.tar.gz
tar zxvf apache-tomcat-7.0.12.tar.gz
mv apache-tomcat-7.0.12 /usr/local/tomcat/
cp –rf /usr/local/tomcat/webapps /data/htdocs/www
vim /usr/local/tomcat/conf/server.xml
修改tomcat的根路径位置,测试网站地址为/data/htdocs/www/shop,这个虚拟主机需要在/usr/local/tomcat/conf/server.xml里指定,改动后的内容如下所示:
unpackWARs="true"autoDeploy="true">
如果我们要继续增加虚拟主机,按照如上格式继续添加内容即可,Host name后面接虚拟主机名称,docBase后面接虚拟主机对应的路径位置。
安装完成后,启动tomcat,默认即监听了8080端口,启动命令如下:
/usr/local/tomcat/bin/startup.sh
成功运行后,用lsof -i:8080来进行验证,如下所示:
lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE NODENAME
java 23731 root 40u IPv6 3347645 TCP *:webcache (LISTEN)
最后安装pcre及Nginx,整合Nginx与Tomcat, Nginx的安装不再详细说明了。静态HTML页面、图片、CSS等由Nginx来处理,jsp、do内容由后端的Tomcat处理,nginx.conf配置文件的内容如下所示(这里为了调试方便,我们首先将域名www.my22.com指向了xxx.xxx.xxx.146,另一台Web配置跟xxx.xxx.xxx.146一样的,我这里以xxx.xxx.xxx.146举例说明,等此架构中的Web环境均顺利后,再将域名www.my22.com指向我们的VIP地址):
user myweb myweb;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /tmp/nginx.pid;
#指定的文件描述符的值最大.
worker_rlimit_nofile 65535;
#工作模式及连接数上限
events
{
use epoll;
worker_connections 65535;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
#设定mime类型
include mime.types;
default_type application/octet-stream;
#charset gb2312;
#设定请求缓冲
server_names_hash_bucket_size128;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 4 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascripttext/css application/xml;
gzip_vary on;
#禁止通过ip访问站点
server{
server_name _;
return 404;
}
server
{
listen 80;
server_name www.my22.com;
index index.html index.htmindex.jsp index.do; #设定访问的默认首页地址
root /data/htdocs/www/shop; #设定网站的资源存放路径
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$http://$host/$1$2/ permanent;
}
#所有jsp的页面均交由tomcat处理
location ~\.(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:8080; #转向tomcat处理
}
location ~ .*\.(htm|html|gif|jpg|jpeg|bmp|swf|ioc|rar|zip|txt|flv|doc|ppt|pdf|xls|mp3|wma)$
#设定访问静态文件直接读取不经过tomcat
{
expires 30d;
}
location ~.*\.(js|css)?$
{
expires 1h;
}
#定义访问日志的写入格式
log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent"$http_x_forwarded_for';
access_log /var/log/www_my22.log wwwlog; #设定访问日志的存放路径
}
}
配置完成后,我们用如下命令启动Nginx进程,让其监听80端口,如下所示:
/usr/local/nginx/nginx
然后我们在/data/htdocs/www/shop/下面写一段Java代码,验证以上的配置是否成功,写完后我们可以用cat 命令来查看/data/htdocs/www/shop/mem.jsp文件,文件内容如下所示:
<%
Runtime lRuntime = Runtime.getRuntime();
out.println("*** BEGIN MEMORY STATISTICS ***
");
out.println("Free Memory:"+lRuntime.freeMemory()+"
");
out.println("Max Memory:"+lRuntime.maxMemory()+"
");
out.println("Total Memory:"+lRuntime.totalMemory()+"
");
out.println("Available Processors : "+lRuntime.availableProcessors()+"
");
out.println("*** END MEMORY STATISTICS ***");
%>
我们可以输入http://www.my22.com/mem.jsp来验证nginx+tomcat整合成功了。
简约配置如下:
user myweb myweb;
worker_processes 1;
error_log off;
pid /tmp/nginx.pid;
events {
use epoll;
worker_connections 10;
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size128;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
tcp_nodelay on;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size128k;
upstream tomcat_server{
server 127.0.0.1:8080;
}
server {
listen xxx.xxx.xxx.146:80;
server_name xxx.xxx.xxx.146 default;
index index.html index.htmindex.jsp index.do;
root /data/htdocs/www/shop;
if (-d $request_filename)
{
rewrite^/(.*)([^/])$ http://$host/$1$2/permanent;
}
location ~\.(jsp|jspx|do)?$ {
proxy_set_headerHost $host;
proxy_set_headerX-Forwarded_For $remote_addr;
proxy_pass http://tomcat_server;
}
error_page 404 500 502 503 504 /50x.html;
location /50x.html{
root /usr/local/nginx/html;
}
}
}
参考网站:
http://os.51cto.com/art/201108/281364.htm
http://blog.csdn.net/tricky1997/article/details/6969599