下载所需软件
tomcat
[root@node2 src]# wget http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.40/bin/apache-tomcat-7.0.40.tar.gz
jdk
[root@node2 src]# wget http://download.oracle.com/otn-pub/java/jdk/7u21-b11/jdk-7u21-linux-x64.tar.gz
安装所需环境
[root@node2 nginx-1.2.6]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libevent libevent-devel perl-ExtUtils-* pcre-*
[root@node2 src]# tar -xf jdk-7u21-linux-x64.gz -C /usr/local/
配置java运行环境
[root@node2 src]# vim /etc/profile.d/jdk.sh
JAVA_HOME=/usr/local/jdk1.7.0_21
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
[root@node2 src]# chmod +x /etc/profile.d/jdk.sh
[root@node2 src]# source /etc/profile.d/jdk.sh
[root@node2 src]# tar xf apache-tomcat-7.0.40.tar.gz -C /usr/local/
[root@node2 src]# cd /usr/local/apache-tomcat-7.0.40/
启动tomcat
[root@node2 apache-tomcat-7.0.40]# bin/startup.sh
Using CATALINA_BASE: /usr/local/apache-tomcat-7.0.40
Using CATALINA_HOME: /usr/local/apache-tomcat-7.0.40
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-7.0.40/temp
Using JRE_HOME: /usr/local/jdk1.7.0_21
Using CLASSPATH: /usr/local/apache-tomcat-7.0.40/bin/bootstrap.jar:/usr/local/apache-tomcat-7.0.40/bin/tomcat-juli.jar
安装与配置nginx
[root@node2 src]# tar xf nginx-1.2.6.tar.gz
[root@node2 src]# cd nginx-1.2.6
[root@node2 nginx-1.2.6]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre
[root@node2 nginx-1.2.6]# make
[root@node2 nginx-1.2.6]# make install
[root@node1 nginx-1.2.6]# cd /usr/local/nginx/
[root@node1 nginx]# cp conf/nginx.conf conf/nginx.conf.bk
配置nginx
[root@node1 nginx]# vim conf/nginx.conf
user www www;
worker_processes 4;
#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 tomcat_server {
server 127.0.0.1:8080; // 定义负载均衡服务器池,名字是自定义的,在这里为tomcat_server
} // 有多台服务器的话,可以一行写一个
server
{
listen 80;
server_name localhost;
index index.html index.htm index.jsp;
root /usr/local/apache-tomcat-7.0.40/webapps;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
location ~ \.(jsp|jspx|do)?$ { // 配置的重点在这里,动态请求转发到后端的tomcat_server服务器池处理
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_server;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 1h;
access_log off;
}
}
}
测试配置是否有问题
[root@node1 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
开启服务
[root@node1 nginx]# sbin/nginx
查看服务是否已经启动
[root@node1 nginx]# netstat -nultp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6997/nginx
创建一个jsp文件做测试
[root@node1 ROOT]# rm -rf /usr/local/apache-tomcat-7.0.40/webapps/ROOT/*
[root@node1 ROOT]# vim index.jsp
<HTML>
<BODY>
<center>
Now time is : <%=new java.util.Date()%>
</center>
</BODY>
</HTML>
打开浏览器测试:
总结:nginx 本身是不支持解析jsp 网页的,当用户的请求来到本机,静态页面便通过nginx处理,也即是80端口处理,而动态页面(在这个例子中指的是jsp)便通过代理,转发到8080端口进行处理,处理完再将数据返还给用户。这样实现了动静分离的效果。
思考:
1、tomcat 本身也可以支持解析静态页面,那么当访问的一个网页是动静态都有的时候,如何确定静态的是通过web服务器(例如apache、nginx)处理的,而不是tomcat处理的?
答:可以访问一个纯静态页面,然后将nginx停止服务,测试还能不能访问到,如果访问不到,说明需要nginx的支持
在开启了nginx和jdk 服务的情况下,访问是能正常访问的:
关闭nginx之后,访问不到:
[root@node1 nginx]# kill `cat /usr/local/nginx/logs/nginx.pid`
2、如何修改tomcat默认网站根目录
tomcat 默认的目录是tomcat工作目录下的webapps/ 目录,但有时我们需要自定义网站根目录,这就需要修改tomcat 的配置文件:
[root@node1 nginx]# vim /usr/local/apache-tomcat-7.0.40/conf/server.xml
找到appBase ,添加最下面一行,在docBase 中指定根目录路径
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/data/www/" />
保存退出后,重启tomcat 服务即可
[root@node1 nginx]# /usr/local/apache-tomcat-7.0.40/bin/shutdown.sh
[root@node1 nginx]# /usr/local/apache-tomcat-7.0.40/bin/startup.sh